當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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<String> de_que = new LinkedList<String>(); 
  
        // 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

程序2:

// 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<Integer> de_que = new LinkedList<Integer>(); 
  
        // 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<String> de_que = new ArrayDeque<String>(); 
  
        // 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<String> de_que = new LinkedBlockingDeque<String>(); 
  
        // 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<String> de_que = new ConcurrentLinkedDeque<String>(); 
  
        // 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://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#descendingIterator-



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Deque descendingIterator() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。