NavigableSet subSet() method in Java

The headSet() method of NavigableSet interface in Java is used to return a view of the portion of this set whose elements range from fromElement to toElement.

  • If fromElement and toElement are equal, the returned set is empty unless fromInclusive and toInclusive are both true.
  • The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.
  • The returned set supports all optional set operations that this set supports.

Note: The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Syntax:



NavigableSet<E> subSet( E fromElement,
                       boolean fromInclusive,
                       E toElement,
                       boolean toInclusive);

Where, E is the type of elements maintained by this Set container.

Parameters: This function accepts 4 parameters as explained below:

  • fromElement: This is a mandatory parameter and specifies the low end point of the returned set.
  • fromInclusive: This is an optional parameter and is of boolean type. If this is set to True, then low end point will be included in the returned view otherwise not.
  • toElement: his is a mandatory parameter and specifies the high end point of the returned set.
  • toInclusive: This is optional parameter and is of boolean type. If this is set to True, then low end point will be included in the returned view otherwise not.

Return Value: It returns a view of the portion of this set whose elements range from fromElement to toElement.

Below programs illustrate the subSet() method in Java:

Program 1: NavigableSet with integer elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to demonstrate
// subSet() method of NavigableSet
  
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<Integer> ns = new TreeSet<>();
        ns.add(0);
        ns.add(1);
        ns.add(2);
        ns.add(3);
        ns.add(4);
        ns.add(5);
        ns.add(6);
  
        System.out.println("Map with key-value between the given argument : "
                                                           + ns.subSet(1, 6));
  
        System.out.println("Map with key-value between the given argument : "
                                                           + ns.subSet(2, 6));
    }
}

chevron_right


Output:

Map with key-value between the given argument : [1, 2, 3, 4, 5]
Map with key-value between the given argument : [2, 3, 4, 5]

Program 2: NavigableSet with string elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to demonstrate
// subSet() method of NavigableSet
  
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<String> ns = new TreeSet<>();
        ns.add("A");
        ns.add("B");
        ns.add("C");
        ns.add("D");
        ns.add("E");
        ns.add("F");
        ns.add("G");
  
        System.out.println("Map with key-value between the given range : " 
                                                   + ns.subSet("B", "G"));
    }
}

chevron_right


Output:

Map with key-value between the given range : [B, C, D, E, F]

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableSet.html#subSet(E, E)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.