📌  相关文章
📜  Java中的LinkedBlockingDeque descendingIterator() 方法

📅  最后修改于: 2022-05-13 01:54:34.158000             🧑  作者: Mango

Java中的LinkedBlockingDeque descendingIterator() 方法

LinkedBlockingDequedescendingIterator()方法以相反的顺序返回此双端队列中元素的迭代器。元素将按从 last(tail) 到 first(head) 的顺序返回。返回的迭代器是一个“弱一致”的迭代器。

句法:

public Iterator descendingIterator()

参数:此方法不接受任何参数。

返回:此方法以相反的顺序返回此双端队列中元素的迭代器。

下面的程序说明了LinkedBlockingDeque的 descendingIterator() 方法:

方案一:

// Java Program Demonstrate descendingIterator()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
  
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to front of LinkedBlockingDeque
        LBD.addFirst(7855642);
        LBD.addFirst(35658786);
        LBD.addFirst(5278367);
        LBD.addFirst(74381793);
  
        // Call descendingIterator() method of LinkedBlockingDeque
        Iterator iteratorVals = LBD.descendingIterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of LinkedBlockingDeque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of LinkedBlockingDeque are:
7855642
35658786
5278367
74381793

方案二:

// Java Program Demonstrate descendingIterator()
// method of LinkedBlockingDeque
// when list is of strings
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
  
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to front of LinkedBlockingDeque
        LBD.add("Geeks");
        LBD.add("forGeeks");
        LBD.add("A");
        LBD.add("Computer");
        LBD.add("Portal");
  
        // Call iterator() method of LinkedBlockingDeque
        Iterator iteratorVals = LBD.descendingIterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of LinkedBlockingDeque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of LinkedBlockingDeque are:
Portal
Computer
A
forGeeks
Geeks

参考: https: Java/util/concurrent/LinkedBlockingDeque.html#descendingIterator()