📜  Java中的Deque descendingIterator() 方法

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

Java中的Deque descendingIterator() 方法

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

句法:

Iterator descendingIterator()

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

返回值:此方法以正确的顺序返回此双端队列中元素的迭代器

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

程序 1:LinkedList的帮助下。

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new LinkedList();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

方案二:

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new LinkedList();
  
        // Use add() method to add elements into the Queue
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of deque are:
10
15
30
20
5

程序 3:ArrayDeque的帮助下。

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new ArrayDeque();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

程序 4:LinkedBlockingDeque的帮助下。

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new LinkedBlockingDeque();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

程序 5:ConcurrentLinkedDeque的帮助下。

// Java code to illustrate descendingIterator()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new ConcurrentLinkedDeque();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Call iterator() method of deque
        Iterator iteratorVals = de_que.iterator();
  
        // Print elements of iterator
        // created from PriorityBlockingQueue
        System.out.println("The iterator values"
                           + " of deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of deque are:
Welcome
To
Geeks
4
Geeks

参考: https: Java/util/Deque.html#descendingIterator–