TreeSet descendingIterator() method in Java with Examples

18 Aug, 2021
The descendingIterator() method of java.util.TreeSet<E> class is used to return an iterator over the elements in this set in descending order.Syntax:  
public Iterator descendingIterator()
Return Value: This method returns an iterator over the elements in this set in descending order.Below are the examples to illustrate the descendingIterator() methodExample 1:  
Java
// 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 integrated 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
 
Example 2:  
Java
// 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 integrated 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
 
kapoorsagar226
Java - util package
Java-Collections
Java-Functions
java-treeset
Articles
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Time Complexity and Space Complexity
SQL Interview Questions
Understanding "extern" keyword in C
SQL | Views
Java Tutorial
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java