Java : ConcurrentSkipListSet with Examples

ConcurrentSkipListSet (Java SE 20 & JDK 20) API Examples.
You will find code examples on most ConcurrentSkipListSet methods.


Summary

A scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap. The elements of the set are kept sorted according to their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

Class diagram

void test(Set<String> set) throws InterruptedException {
    set.add("a");
    set.add("b");
    set.add("c");
    set.add("d");
    set.add("e");

    try (final var executor = Executors.newSingleThreadScheduledExecutor()) {

        executor.schedule(() -> {
            System.out.println("-- add value! --");
            set.add("x");
        }, 5, TimeUnit.SECONDS);

        try {
            for (final var value : set) {
                System.out.println("value = " + value);
                TimeUnit.SECONDS.sleep(2);
            }
        } catch (ConcurrentModificationException e) {
            System.out.println("ConcurrentModificationException!");
        }
    }

    System.out.println("-- end --");
    System.out.println("set = " + set);
}
test(new TreeSet<>());

// Result
// ↓
//value = a
//value = b
//value = c
//-- add value! --
//ConcurrentModificationException!
//-- end --
//set = [a, b, c, d, e, x]

test(new ConcurrentSkipListSet<>());

// Result
// ↓
//value = a
//value = b
//value = c
//-- add value! --
//value = d
//value = e
//value = x
//-- end --
//set = [a, b, c, d, e, x]
final var navigableSet = new ConcurrentSkipListSet<String>();
navigableSet.add("ccc");
navigableSet.add("ddd");
navigableSet.add("aaa");
navigableSet.add("bbb");

System.out.println(navigableSet); // [aaa, bbb, ccc, ddd]

final var hashSet = ConcurrentHashMap.<String>newKeySet();
hashSet.add("ccc");
hashSet.add("ddd");
hashSet.add("aaa");
hashSet.add("bbb");

System.out.println(hashSet); // [aaa, ccc, bbb, ddd]

Constructors

ConcurrentSkipListSet ()

Constructs a new, empty set that orders its elements according to their natural ordering.

final var set = new ConcurrentSkipListSet<String>();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true

set.add("b");
set.add("a");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
final var set = new ConcurrentSkipListSet<Integer>();

set.add(100);
set.add(999);
set.add(-200);

System.out.println(set); // [-200, 100, 999]

ConcurrentSkipListSet (Collection<? extends E> c)

Constructs a new set containing the elements in the specified collection, that orders its elements according to their natural ordering.

final var c = List.of("b", "a", "c");

final var set = new ConcurrentSkipListSet<>(c);
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3

ConcurrentSkipListSet (Comparator<? super E> comparator)

Constructs a new, empty set that orders its elements according to the specified comparator.

final var src = List.of("bbb", "aaa", "ccc");

final var set1 = new ConcurrentSkipListSet<String>();
set1.addAll(src);
System.out.println(set1); // [aaa, bbb, ccc]

final var set2 = new ConcurrentSkipListSet<String>(Comparator.reverseOrder());
set2.addAll(src);
System.out.println(set2); // [ccc, bbb, aaa]

ConcurrentSkipListSet (SortedSet<E> s)

Constructs a new set containing the same elements and using the same ordering as the specified sorted set.

final var s = new ConcurrentSkipListSet<String>(Comparator.reverseOrder());
s.add("a");
s.add("b");
s.add("c");

System.out.println(s); // [c, b, a]

final var set = new ConcurrentSkipListSet<>(s);
System.out.println(set); // [c, b, a]
System.out.println(set.size()); // 3

Methods

boolean add (E e)

Adds the specified element to this set if it is not already present.

final var set = new ConcurrentSkipListSet<String>();
System.out.println(set); // []

System.out.println(set.add("a")); // true
System.out.println(set); // [a]

System.out.println(set.add("b")); // true
System.out.println(set); // [a, b]

System.out.println(set.add("c")); // true
System.out.println(set); // [a, b, c]

System.out.println(set.add("a")); // false
System.out.println(set); // [a, b, c]

E ceiling (E e)

Returns the least element in this set greater than or equal to the given element, or null if there is no such element.

final var set = new ConcurrentSkipListSet<String>();
set.add("b");
set.add("d");
set.add("f");

System.out.println(set); // [b, d, f]

System.out.println(set.ceiling("a")); // b
System.out.println(set.ceiling("b")); // b
System.out.println(set.ceiling("c")); // d
System.out.println(set.ceiling("d")); // d
System.out.println(set.ceiling("e")); // f
System.out.println(set.ceiling("f")); // f
System.out.println(set.ceiling("g")); // null

void clear ()

Removes all of the elements from this set.

final var set = new ConcurrentSkipListSet<String>();
System.out.println(set); // []

set.add("a");
System.out.println(set); // [a]

set.add("b");
System.out.println(set); // [a, b]

set.clear();
System.out.println(set); // []

ConcurrentSkipListSet<E> clone ()

Returns a shallow copy of this ConcurrentSkipListSet instance.

final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var cloned = set.clone();
System.out.println(cloned); // [a, b, c]
System.out.println(cloned.getClass()); // class java.util.concurrent.ConcurrentSkipListSet

Comparator<? super E> comparator ()

Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.

final var set = new ConcurrentSkipListSet<String>();
System.out.println(set); // []

final var comparator = set.comparator();
System.out.println(comparator); // null
final var set = new ConcurrentSkipListSet<String>(Comparator.reverseOrder());
System.out.println(set); // []

final var comparator = set.comparator();
System.out.println(Objects.equals(comparator, Comparator.reverseOrder())); // true
final var set = new ConcurrentSkipListSet<>(String.CASE_INSENSITIVE_ORDER);
System.out.println(set); // []

final var comparator = set.comparator();
System.out.println(Objects.equals(comparator, String.CASE_INSENSITIVE_ORDER)); // true

boolean contains (Object o)

Returns true if this set contains the specified element.

final var set = new ConcurrentSkipListSet<String>();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

System.out.println(set.contains("")); // false
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("c")); // true

System.out.println(set.contains("X")); // false
System.out.println(set.contains("abc")); // false

Iterator<E> descendingIterator ()

Returns an iterator over the elements in this set in descending order.

final var set = new ConcurrentSkipListSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

final var iterator = set.descendingIterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(s -> {
    System.out.println(s);
});

// Result
// ↓
//-- forEachRemaining --
//ccc
//bbb
//aaa

NavigableSet<E> descendingSet ()

Returns a reverse order view of the elements contained in this set.

final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var descSet = set.descendingSet();
System.out.println(descSet); // [c, b, a]

set.add("d");

System.out.println(set); // [a, b, c, d]
System.out.println(descSet); // [d, c, b, a]

descSet.remove("b");

System.out.println(set); // [a, c, d]
System.out.println(descSet); // [d, c, a]

boolean equals (Object o)

Compares the specified object with this set for equality.

final var set = new ConcurrentSkipListSet<String>();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

System.out.println(set.equals(Set.of("a"))); // false
System.out.println(set.equals(Set.of("a", "b"))); // false
System.out.println(set.equals(Set.of("a", "b", "c"))); // true
System.out.println(set.equals(Set.of("c", "b", "a"))); // true
final var set = new ConcurrentSkipListSet<String>();
Collections.addAll(set, "a", "b", "c");
System.out.println(set); // [a, b, c]

final var hashSet = new HashSet<>(set);
System.out.println(hashSet); // [a, b, c]
System.out.println(set.equals(hashSet)); // true

final var treeSet = new TreeSet<>(set);
System.out.println(treeSet); // [a, b, c]
System.out.println(set.equals(treeSet)); // true

final var list = new ArrayList<>(set);
System.out.println(list); // [a, b, c]
System.out.println(set.equals(list)); // false

E first ()

Returns the first (lowest) element currently in this set.

final var set = new ConcurrentSkipListSet<String>();
System.out.println(set); // []

set.add("b");

System.out.println(set); // [b]
System.out.println(set.first()); // b

set.add("c");

System.out.println(set); // [b, c]
System.out.println(set.first()); // b

set.add("a");

System.out.println(set); // [a, b, c]
System.out.println(set.first()); // a
final var set = new ConcurrentSkipListSet<String>();
System.out.println(set); // []

try {
    set.first();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

E floor (E e)

Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.

final var set = new ConcurrentSkipListSet<String>();
set.add("b");
set.add("d");
set.add("f");

System.out.println(set); // [b, d, f]

System.out.println(set.floor("a")); // null
System.out.println(set.floor("b")); // b
System.out.println(set.floor("c")); // b
System.out.println(set.floor("d")); // d
System.out.println(set.floor("e")); // d
System.out.println(set.floor("f")); // f
System.out.println(set.floor("g")); // f

NavigableSet<E> headSet (E toElement)

Returns a view of the portion of this set whose elements are strictly less than toElement.

final var set = new ConcurrentSkipListSet<String>();
set.add("x");
set.add("y");
set.add("z");

System.out.println(set); // [x, y, z]

final var headSet = set.headSet("z");
System.out.println(headSet); // [x, y]

set.add("a");

System.out.println(set); // [a, x, y, z]
System.out.println(headSet); // [a, x, y]

headSet.remove("x");

System.out.println(set); // [a, y, z]
System.out.println(headSet); // [a, y]
final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.headSet("a")); // []
System.out.println(set.headSet("b")); // [a]
System.out.println(set.headSet("c")); // [a, b]
System.out.println(set.headSet("d")); // [a, b, c]

NavigableSet<E> headSet (E toElement, boolean inclusive)

Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.

final var set = new ConcurrentSkipListSet<String>();
set.add("x");
set.add("y");
set.add("z");

System.out.println(set); // [x, y, z]

final var headSet = set.headSet("y", true);
System.out.println(headSet); // [x, y]

set.add("a");

System.out.println(set); // [a, x, y, z]
System.out.println(headSet); // [a, x, y]

headSet.remove("x");

System.out.println(set); // [a, y, z]
System.out.println(headSet); // [a, y]
final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.headSet("a")); // []
System.out.println(set.headSet("a", true)); // [a]

System.out.println(set.headSet("b")); // [a]
System.out.println(set.headSet("b", true)); // [a, b]

System.out.println(set.headSet("c")); // [a, b]
System.out.println(set.headSet("c", true)); // [a, b, c]

System.out.println(set.headSet("d")); // [a, b, c]
System.out.println(set.headSet("d", true)); // [a, b, c]

E higher (E e)

Returns the least element in this set strictly greater than the given element, or null if there is no such element.

final var set = new ConcurrentSkipListSet<String>();
set.add("b");
set.add("d");
set.add("f");

System.out.println(set); // [b, d, f]

System.out.println(set.higher("a")); // b
System.out.println(set.higher("b")); // d
System.out.println(set.higher("c")); // d
System.out.println(set.higher("d")); // f
System.out.println(set.higher("e")); // f
System.out.println(set.higher("f")); // null
System.out.println(set.higher("g")); // null

boolean isEmpty ()

Returns true if this set contains no elements.

final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.isEmpty()); // false

set.clear();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true

Iterator<E> iterator ()

Returns an iterator over the elements in this set in ascending order.

final var set = new ConcurrentSkipListSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

final var iterator = set.iterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(s -> {
    System.out.println(s);
});

// Result
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc

E last ()

Returns the last (highest) element currently in this set.

final var set = new ConcurrentSkipListSet<String>();
System.out.println(set); // []

set.add("b");

System.out.println(set); // [b]
System.out.println(set.last()); // b

set.add("a");

System.out.println(set); // [a, b]
System.out.println(set.last()); // b

set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.last()); // c
final var set = new ConcurrentSkipListSet<String>();
System.out.println(set); // []

try {
    set.last();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

E lower (E e)

Returns the greatest element in this set strictly less than the given element, or null if there is no such element.

final var set = new ConcurrentSkipListSet<String>();
set.add("b");
set.add("d");
set.add("f");

System.out.println(set); // [b, d, f]

System.out.println(set.lower("a")); // null
System.out.println(set.lower("b")); // null
System.out.println(set.lower("c")); // b
System.out.println(set.lower("d")); // b
System.out.println(set.lower("e")); // d
System.out.println(set.lower("f")); // d
System.out.println(set.lower("g")); // f

E pollFirst ()

Retrieves and removes the first (lowest) element, or returns null if this set is empty.

final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.pollFirst()); // a
System.out.println(set); // [b, c]

System.out.println(set.pollFirst()); // b
System.out.println(set); // [c]

System.out.println(set.pollFirst()); // c
System.out.println(set); // []

System.out.println(set.pollFirst()); // null

E pollLast ()

Retrieves and removes the last (highest) element, or returns null if this set is empty.

final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.pollLast()); // c
System.out.println(set); // [a, b]

System.out.println(set.pollLast()); // b
System.out.println(set); // [a]

System.out.println(set.pollLast()); // a
System.out.println(set); // []

System.out.println(set.pollLast()); // null

boolean remove (Object o)

Removes the specified element from this set if it is present.

final var src = Set.of("a", "b", "c");

{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("")); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("a")); // true
    System.out.println(set); // [b, c]
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("b")); // true
    System.out.println(set); // [a, c]
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("X")); // false
    System.out.println(set); // [a, b, c]
}

boolean removeAll (Collection<?> c)

Removes from this set all of its elements that are contained in the specified collection.

final var src = Set.of("a", "b", "c");

{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of())); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a"))); // true
    System.out.println(set); // [b, c]
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "b"))); // true
    System.out.println(set); // [c]
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("b", "a"))); // true
    System.out.println(set); // [c]
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "b", "c"))); // true
    System.out.println(set); // []
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("X", "Y"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final var set = new ConcurrentSkipListSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "X"))); // true
    System.out.println(set); // [b, c]
}

int size ()

Returns the number of elements in this set.

final var set1 = new ConcurrentSkipListSet<String>();
System.out.println(set1); // []
System.out.println(set1.size()); // 0

final var set2 = new ConcurrentSkipListSet<String>();
Collections.addAll(set2, "a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3

Spliterator<E> spliterator ()

Returns a Spliterator over the elements in this set.

final var set = new ConcurrentSkipListSet<String>();
set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

final var spliterator = set.spliterator();

System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(s -> {
    System.out.println(s);
});

// Result
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc

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

Returns a view of the portion of this set whose elements range from fromElement to toElement.

final var set = new ConcurrentSkipListSet<String>();
set.add("x");
set.add("y");
set.add("z");

System.out.println(set); // [x, y, z]

final var subSet = set.subSet("a", true, "y", true);
System.out.println(subSet); // [x, y]

set.add("a");

System.out.println(set); // [a, x, y, z]
System.out.println(subSet); // [a, x, y]

subSet.remove("x");

System.out.println(set); // [a, y, z]
System.out.println(subSet); // [a, y]
final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.subSet("a", true, "c", true)); // [a, b, c]
System.out.println(set.subSet("a", true, "c", false)); // [a, b]
System.out.println(set.subSet("a", false, "c", true)); // [b, c]
System.out.println(set.subSet("a", false, "c", false)); // [b]

System.out.println(set.subSet("a", true, "a", false)); // []
System.out.println(set.subSet("a", true, "b", false)); // [a]
System.out.println(set.subSet("a", true, "c", false)); // [a, b]
System.out.println(set.subSet("a", true, "d", false)); // [a, b, c]

System.out.println(set.subSet("a", true, "d", false)); // [a, b, c]
System.out.println(set.subSet("b", true, "d", false)); // [b, c]
System.out.println(set.subSet("c", true, "d", false)); // [c]
System.out.println(set.subSet("d", true, "d", false)); // []

NavigableSet<E> subSet (E fromElement, E toElement)

Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

final var set = new ConcurrentSkipListSet<String>();
set.add("x");
set.add("y");
set.add("z");

System.out.println(set); // [x, y, z]

final var subSet = set.subSet("a", "z");
System.out.println(subSet); // [x, y]

set.add("a");

System.out.println(set); // [a, x, y, z]
System.out.println(subSet); // [a, x, y]

subSet.remove("x");

System.out.println(set); // [a, y, z]
System.out.println(subSet); // [a, y]
final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.subSet("a", "a")); // []
System.out.println(set.subSet("a", "b")); // [a]
System.out.println(set.subSet("a", "c")); // [a, b]
System.out.println(set.subSet("a", "d")); // [a, b, c]

System.out.println(set.subSet("a", "d")); // [a, b, c]
System.out.println(set.subSet("b", "d")); // [b, c]
System.out.println(set.subSet("c", "d")); // [c]
System.out.println(set.subSet("d", "d")); // []

NavigableSet<E> tailSet (E fromElement)

Returns a view of the portion of this set whose elements are greater than or equal to fromElement.

final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var tailSet = set.tailSet("b");
System.out.println(tailSet); // [b, c]

set.add("d");

System.out.println(set); // [a, b, c, d]
System.out.println(tailSet); // [b, c, d]

tailSet.remove("b");

System.out.println(set); // [a, c, d]
System.out.println(tailSet); // [c, d]
final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.tailSet("a")); // [a, b, c]
System.out.println(set.tailSet("b")); // [b, c]
System.out.println(set.tailSet("c")); // [c]
System.out.println(set.tailSet("d")); // []

NavigableSet<E> tailSet (E fromElement, boolean inclusive)

Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var tailSet = set.tailSet("a", false);
System.out.println(tailSet); // [b, c]

set.add("d");

System.out.println(set); // [a, b, c, d]
System.out.println(tailSet); // [b, c, d]

tailSet.remove("b");

System.out.println(set); // [a, c, d]
System.out.println(tailSet); // [c, d]
final var set = new ConcurrentSkipListSet<String>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

System.out.println(set.tailSet("a")); // [a, b, c]
System.out.println(set.tailSet("a", false)); // [b, c]

System.out.println(set.tailSet("b")); // [b, c]
System.out.println(set.tailSet("b", false)); // [c]

System.out.println(set.tailSet("c")); // [c]
System.out.println(set.tailSet("c", false)); // []

Methods declared in AbstractSet

hashCode

Please see the link below.

Methods declared in AbstractCollection

addAll, containsAll, retainAll, toArray, toArray, toString

Please see the link below.

Methods declared in Collection

parallelStream, removeIf, stream, toArray

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.

Methods declared in Set

addAll, containsAll, hashCode, retainAll, toArray, toArray

Please see the link below.


Related posts

To top of page