TreeSet descendingIterator() in Java

descendingIterator(): This method is available in java.util.TreeSet class of Java.

Syntax:

Iterator<V> java.util.TreeSet.descendingIterator()

This method returns an iterator over the elements in this set in descending order.

Parameters: NA

Returns: an iterator over the elements in this set in descending order.

Exceptions: NA

Approach

Java

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetdescendingIterator {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        Iterator<String> iterator = treeSet.descendingIterator();

        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");

        }

    }
}

Output:

Program Java Hello C++ 

No comments:

Post a Comment