Java Program to get the reverse of the NavigableSet

Java 8Object Oriented ProgrammingProgramming

We will create a NavigableSet collection and add some elements −

NavigableSet<String> set = new TreeSet<>();
set.add("ABC");
set.add("DEF");
set.add("GHI");
set.add("JKL");
set.add("MNO");
set.add("PQR");
set.add("STU");

Now, use descendingSet() for the reverse of NavigableSet −

NavigableSet<String>reverse = set.descendingSet();

Example

 Live Demo

import java.util.NavigableSet;
import java.util.TreeSet;
public class Demo {
   public static void main(String[] args) {
      NavigableSet<String> set = new TreeSet<>();
      set.add("ABC");
      set.add("DEF");
      set.add("GHI");
      set.add("JKL");
      set.add("MNO");
      set.add("PQR");
      set.add("STU");
      NavigableSet<String>reverse = set.descendingSet();
      System.out.println("NavigableSet = " + set);
      System.out.println("NavigableSet (Reverse) = " + reverse);
   }
}

Output

NavigableSet = [ABC, DEF, GHI, JKL, MNO, PQR, STU]
NavigableSet (Reverse) = [STU, PQR, MNO, JKL, GHI, DEF, ABC]
raja
Published on 25-Apr-2019 09:46:51
Advertisements