How to use subSet() method of Java NavigableSet Class

Java 8Object Oriented ProgrammingProgramming

Use the subset() method to get elements from a limit. At first, create NavigableSet and add elements −

NavigableSet<Integer> set = new TreeSet<>();
set.add(10);
set.add(25);
set.add(40);
set.add(55);
set.add(70);
set.add(85);

Now, use the subset() method −

set.subSet(40, 85)

The following is an example to implement subset() method of Java NaviagbleSet class −

Example

 Live Demo

import java.util.NavigableSet;
import java.util.TreeSet;
public class Demo {
   public static void main(String[] args) {
      NavigableSet<Integer> set = new TreeSet<>();
      set.add(10);
      set.add(25);
      set.add(40);
      set.add(55);
      set.add(70);
      set.add(85);
      set.add(100);
      System.out.println("Returned Value = " + set.subSet(40, 85));
   }
}

Output

Returned Value = [40, 55, 70]
raja
Published on 11-Jan-2019 07:19:13
Advertisements