ConcurrentSkipListSet subSet() method in Java

Last Updated : 14 Oct, 2026

The subSet() method of the ConcurrentSkipListSet class in Java returns a view containing elements within a specified range. The elements remain sorted, and the method allows us to control whether the lower and upper boundaries are included.

  • Returns a NavigableSet view of elements within the specified range.
  • Supports both inclusive and exclusive range boundaries.
  • Changes made to the subset are reflected in the original set and vice versa.

Syntax

The subSet() method has two overloaded forms:

// Lower bound inclusive, upper bound exclusive
public NavigableSet<E> subSet(E fromElement, E toElement)

// Custom inclusive and exclusive boundaries
public NavigableSet<E> subSet(
E fromElement,
boolean fromInclusive,
E toElement,
boolean toInclusive
)

Parameters

  • fromElement: Specifies the lower boundary of the subset.
  • toElement: Specifies the upper boundary of the subset.
  • fromInclusive: true includes the lower boundary; false excludes it.
  • toInclusive: true includes the upper boundary; false excludes it.

Return Value: Returns a NavigableSet<E> representing a backed view of elements within the specified range.

1. subSet(E fromElement, E toElement)

This overload returns elements greater than or equal to fromElement and strictly less than toElement.

It is equivalent to:

subSet(fromElement, true, toElement, false);

Example: The following program creates a subset containing elements from 20 (inclusive) to 50 (exclusive).

Java
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;

class Geeks{
    
    public static void main(String[] args){
        
        ConcurrentSkipListSet<Integer> set
            = new ConcurrentSkipListSet<>();

        set.add(10);
        set.add(20);
        set.add(30);
        set.add(40);
        set.add(50);
        set.add(60);

        NavigableSet<Integer> subset = set.subSet(20, 50);

        System.out.println("Original Set: " + set);
        System.out.println("Subset: " + subset);
    }
}

Output
Original Set: [10, 20, 30, 40, 50, 60]
Subset: [20, 30, 40]

Explanation: The subSet(20, 50) method includes 20 but excludes 50. Therefore, the returned subset contains 20, 30, and 40.

2. subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

This overload provides more control over range boundaries. The boolean parameters specify whether the lower and upper boundaries should be included.

Example: The following program returns a subset that includes both 20 and 50.

Java
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;

class Geeks{
    
    public static void main(String[] args){
        
        ConcurrentSkipListSet<Integer> set =
            new ConcurrentSkipListSet<>();

        set.add(10);
        set.add(20);
        set.add(30);
        set.add(40);
        set.add(50);
        set.add(60);

        NavigableSet<Integer> subset =
            set.subSet(20, true, 50, true);

        System.out.println("Original Set: " + set);
        System.out.println("Subset: " + subset);
    }
}

Output
Original Set: [10, 20, 30, 40, 50, 60]
Subset: [20, 30, 40, 50]

Explanation: Both fromInclusive and toInclusive are set to true. Therefore, the subset includes both boundary values, 20 and 50.

Different Boundary Combinations

The following table shows how different inclusive and exclusive boundaries affect the returned subset.

Assume the original set contains [10, 20, 30, 40, 50, 60].

Method

Result

subSet(20, true, 50, true)

[20, 30, 40, 50]

subSet(20, true, 50, false)

[20, 30, 40]

subSet(20, false, 50, true)

[30, 40, 50]

subSet(20, false, 50, false)

[30, 40]

Changes in Subset Affect the Original Set

The subSet() method returns a backed view rather than an independent copy. Therefore, adding or removing elements through the subset modifies the original set. Similarly, changes to the original set within the selected range are reflected in the subset.

Example: The following program demonstrates how changes in the subset affect the original set.

Java
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;

class Geeks{
    
    public static void main(String[] args){
        
        ConcurrentSkipListSet<Integer> set =
            new ConcurrentSkipListSet<>();

        set.add(10);
        set.add(20);
        set.add(30);
        set.add(40);
        set.add(50);

        NavigableSet<Integer> subset = set.subSet(20, 50);

        subset.remove(30);
        subset.add(35);

        System.out.println("Original Set: " + set);
        System.out.println("Subset: " + subset);
    }
}

Output
Original Set: [10, 20, 35, 40, 50]
Subset: [20, 35, 40]

Explanation: Removing 30 and adding 35 through the subset also updates the original set because both share the same backing collection.

Exceptions Thrown by subSet()

The subSet() method can throw the following exceptions when invalid boundaries or incompatible elements are supplied.

Exception

Cause

NullPointerException

Either boundary is null.

IllegalArgumentException

The lower boundary is greater than the upper boundary, or a boundary lies outside the permitted range of an existing restricted view.

ClassCastException

The boundary elements cannot be compared using the set's comparator or natural ordering.

Example: The following program demonstrates an IllegalArgumentException when the lower boundary is greater than the upper boundary.

Java
import java.util.concurrent.ConcurrentSkipListSet;

class Geeks{
    
    public static void main(String[] args){
        
        ConcurrentSkipListSet<Integer> set =
            new ConcurrentSkipListSet<>();

        set.add(10);
        set.add(20);
        set.add(30);
        set.add(40);

        try {
            set.subSet(40, 20);
        } catch (IllegalArgumentException e) {
            System.out.println("Invalid subset range");
        }
    }
}

Output
Invalid subset range

Explanation: The lower boundary (40) is greater than the upper boundary (20). Therefore, the method throws an IllegalArgumentException.

Important Points About subSet()

  • The returned subset maintains the ordering defined by the original set's comparator or natural ordering.
  • The method returns a backed view, not a separate collection.
  • Adding an element outside the subset's allowed range throws an IllegalArgumentException.
  • When both boundaries are equal, the two-parameter overload returns an empty subset. The four-parameter overload can include that boundary only when both inclusion flags are true.
  • ConcurrentSkipListSet supports concurrent access, and its iterators are weakly consistent, meaning they may reflect some changes made during iteration without throwing ConcurrentModificationException.
Comment