Java Program to Iterate TreeSet Collection in Descending Order

You are currently viewing Java Program to Iterate TreeSet Collection in Descending Order

Java program to iterate TreeSet collection in descending order

// Java program to iterate TreeSet collection 
// in descending order

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String args[]) {
    TreeSet < Integer > tree = new TreeSet < Integer > ();

    tree.add(25);
    tree.add(20);
    tree.add(35);
    tree.add(30);

    Iterator < Integer > iterator = tree.descendingIterator();

    System.out.println("Elements in descending order:");

    while (iterator.hasNext()) {
      System.out.println("Value : " + iterator.next());
    }
  }
}

Output

Elements in descending order:
Value : 35
Value : 30
Value : 25
Value : 20

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply