java.util.TreeSet类的descendingIterator()方法用于按降序返回对此集合中的元素的迭代器。
用法:
public Iterator descendingIterator()
返回值:此方法以降序返回此集合中元素的迭代器。
以下示例说明了descendingIterator()方法
示例1:
// Java program to demonstrate
// descendingIterator() method
// for Integer value
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// create tree set object
TreeSet<Integer> treeadd = new TreeSet<Integer>();
// populate the TreeSet using add() method
treeadd.add(10);
treeadd.add(20);
treeadd.add(30);
treeadd.add(40);
// Print the TreeSet
System.out.println("TreeSet: " + treeadd);
// create descending iterator
// using descendingIterator() method
Iterator<Integer>
iterator = treeadd.descendingIterator();
System.out.println("\nValues using DescendingIterator:");
// printing the interated value
while (iterator.hasNext()) {
System.out.println("Value : "
+ iterator.next());
}
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
TreeSet: [10, 20, 30, 40] Values using DescendingIterator: Value : 40 Value : 30 Value : 20 Value : 10
示例2:
// Java program to demonstrate
// descendingIterator() method
// for String value
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// create tree set object
TreeSet<String> treeadd = new TreeSet<String>();
// populate the TreeSet using add() method
treeadd.add("A");
treeadd.add("B");
treeadd.add("C");
treeadd.add("D");
// Print the TreeSet
System.out.println("TreeSet: " + treeadd);
// create descending iterator
// using descendingIterator() method
Iterator<String>
iterator = treeadd.descendingIterator();
System.out.println("\nValues using DescendingIterator:");
// printing the interated value
while (iterator.hasNext()) {
System.out.println("Value : "
+ iterator.next());
}
}
catch (Exception e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
TreeSet: [A, B, C, D] Values using DescendingIterator: Value : D Value : C Value : B Value : A
相关用法
- Java LinkedList descendingIterator()用法及代码示例
- Java TreeSet ceiling()用法及代码示例
- Java TreeSet higher()用法及代码示例
- Java TreeSet descendingSet()用法及代码示例
- Java TreeSet floor()用法及代码示例
- Java Deque descendingIterator()用法及代码示例
- Java LinkedBlockingDeque descendingIterator()用法及代码示例
- Java ConcurrentSkipListSet descendingIterator()用法及代码示例
- Java ArrayDeque descendingIterator()用法及代码示例
- Java ConcurrentLinkedDeque descendingIterator()用法及代码示例
- Java NavigableSet descendingIterator()用法及代码示例
- Java TreeSet contains()用法及代码示例
- Java TreeSet last()用法及代码示例
- Java TreeSet add()用法及代码示例
- Java TreeSet first()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 TreeSet descendingIterator() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。