Friday, August 9, 2019

Java ConcurrentSkipListSet Examples

1. Overview


In this tutorial, We'll guide to the new Java 1.6 version ConcurrentSkipListSet. This is part of the new Collection API and package java.util.concurrent.

This class is a direct subclass of AbstractSet and implements NavigableSet.

public class ConcurrentSkipListSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable


ConcurrentSkipListSet is mainly used to keep the elements sorted according to their natural ordering. This is the default behavior and no need to provide the comparator logic to it. If we provide the Comparator at the time of constructor creation then it uses our logic to sort the set. ConcurrentSkipListSet internally built using ConcurrentSkipListMap. This is equivalent to TreeSet and works well in a concurrent environment.




2. ConcurrentSkipListSet Constructors


This has 4 overloaded constructors.

ConcurrentSkipListSet(): Constructs a new, empty set that orders its elements according to their natural ordering.
ConcurrentSkipListSet(Collection; c): Constructs a new set containing the elements in the specified collection, that orders its elements according to their natural ordering.
ConcurrentSkipListSet(Comparator comparator): Constructs a new, empty set that orders its elements according to the specified comparator.
ConcurrentSkipListSet(SortedSet s): Constructs a new set containing the same elements and using the same ordering as the specified sorted set.

package com.java.w3schools.blog.java.collections;

import java.util.concurrent.ConcurrentSkipListSet;

/**
 * Java ConcurrentSkipListSet Examples
 * 
 * @author Venkatesh
 *
 */
public class ConcurrentSkipListSetExample {

public static void main(String[] args) {

// ConcurrentSkipListSet with natural sorting
ConcurrentSkipListSet<String> concurrentSkipListSet = new ConcurrentSkipListSet<>();
concurrentSkipListSet.add("Y");
concurrentSkipListSet.add("10");
concurrentSkipListSet.add("A");
concurrentSkipListSet.add("N");

System.out.println("concurrentSkipListSet values : " + concurrentSkipListSet);

// ConcurrentSkipListSet with comparator
ConcurrentSkipListSet<String> concurrentSkipListSetComparator = new ConcurrentSkipListSet<>(
(s1, s2) -> s2.compareTo(s1));
concurrentSkipListSetComparator.addAll(concurrentSkipListSet);

System.out.println("concurrentSkipListSetComparator values : " + concurrentSkipListSetComparator);

}

}

Output:

concurrentSkipListSet values : [10, A, N, Y]
concurrentSkipListSetComparator values : [Y, N, A, 10]

3. ConcurrentSkipListSet Method Examples


This class has many methods. We will see the most used methods with an example program.

add(E e): Adds the specified element to this set if it is not already present.
clear(): Removes all of the elements from this set.
contains(Object o): Returns true if this set contains the specified element.
descendingIterator(): Returns an iterator over the elements in this set in descending order.
first(): Returns the first (lowest) element currently in this set.
isEmpty(): Returns true if this set contains no elements.
iterator(): Returns an iterator over the elements in this set in ascending order.
last(): Returns the last (highest) element currently in this set.
remove(Object o): Removes the specified element from this set if it is present.
size(): Returns the number of elements in this set.
spliterator(): Returns a Spliterator over the elements in this set.

3.1 add() Example

Syntax:

public boolean add(E e)

This method does not allow null values. Adding null values will throw NullPointerException.

Example:

// add example
ConcurrentSkipListSet<String> skipSet = new ConcurrentSkipListSet<>();
skipSet.add("Java");
skipSet.add("World");
skipSet.add("Hello");
skipSet.add("Welcome");
skipSet.add("to Java8Example");

System.out.println("Added values: "+skipSet);

Output:

Sorted all added the values of the set using add() method.

Added values: [Hello, Java, Welcome, World, to Java8Example]

3.2 clear() Example

Removes all of the elements from this set.

Syntax:

public void clear()

Example:

skipSet.clear();
System.out.println(skipSet.size());

Output:

0

3.3 contains(Object o)


Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that o.equals(e).

Syntax:

public boolean contains(Object o)

if the passed object is null, it throws NullPointerException.

Example:

boolean found = skipSet.contains("Java");
System.out.println("Java is found in set? " + found);

found = skipSet.contains("Java 8");
System.out.println("Java 8 is found in set? " + found);

Output:

Java is found in set? true
Java 8 is found in set? false


3.4 descendingIterator() Example


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

Syntax:

public Iterator<E> descendingIterator()

Example:

// descendingIterator() example
Iterator it = skipSet.descendingIterator();
while (it.hasNext()) {
System.out.println("Next value: " + it.next());
}

Output:

Next value: to Java8Example
Next value: World
Next value: Welcome
Next value: Java
Next value: Hello

3.5 first() Example


Returns the first (lowest) element currently in this set. This is after sorting the elements.

Syntax:

public E first()

Example:

// first()
String firstValue = skipSet.first();
System.out.println("firstValue : " + firstValue);

Output:

firstValue : Hello

3.6 isEmpty() Example

Returns true if this set contains no elements.

Syntax:

public boolean isEmpty()

Example:

// isEmpty()
boolean isSetEmpty = skipSet.isEmpty();
System.out.println("skipSet empty check : "+isSetEmpty);

Output:

skipSet empty check : false

3.7 iterator() Example


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

Syntax:

public Iterator<E> iterator()


Example:

// iterator() example
Iterator iterator = skipSet.iterator();
while (iterator.hasNext()) {
System.out.println("Natural Next value: " + iterator.next());
}

Output:

Natural Next value: Hello
Natural Next value: Java
Natural Next value: Welcome
Natural Next value: World
Natural Next value: to Java8Example

Compare this output with method descendingIterator() output. This is reverse to that and in natural ascending order.

3.8 last() Example


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

Syntax:

public E last()

Example:

// last()
String lastValue = skipSet.last();
System.out.println("lastValue : " + lastValue);


Output:

lastValue : to Java8Example

3.9 remove(Object o) Example


Removes the specified element from this set if it is present

Syntax:

public boolean remove(Object o)

Returns true if this set contained the specified element, false otherwise.

Example:

boolean javaRemoved = skipSet.remove("Java");
boolean helloRemoved = skipSet.remove("hello");
System.out.println("javaRemoved : " + javaRemoved);
System.out.println("helloRemoved : " + helloRemoved);
System.out.println("Set values after removal : " + skipSet);

Output:

javaRemoved : true
helloRemoved : false
Set values after removal : [Hello, Welcome, World, to Java8Example]

3.10 size() Example


Returns the number of elements in this set. If this set contains more than Integer.MAX_VALUE elements, it returns Integer.MAX_VALUE.

Syntax:

public int size()


Example:

System.out.println("set size : "+skipSet.size());

Output:

set size : 4

3.11 spliterator() Example


Returns a Spliterator over the elements in this set.

The Spliterator reports Spliterator.CONCURRENT, Spliterator.NONNULL, Spliterator.DISTINCT, Spliterator.SORTED and Spliterator.ORDERED, with an encounter order that is ascending order. Overriding implementations should document the reporting of additional characteristic values.

The spliterator's comparator is null if the set's comparator is null. Otherwise, the spliterator's comparator is the same as or imposes the same total ordering as the set's comparator.

Syntax:

public Spliterator<E> spliterator()

Example:

Spliterator<String> spliterator = skipSet.spliterator();
spliterator.forEachRemaining( value -> System.out.println(value));

Output:

Hello
Welcome
World
to Java8Example

4. Conclusion


In this article, We've covered java 6 ConcurrentSkipListSet and its methods add(), remove(), size(), descendingIterator(), spliterator() methods with example programs.

All code shown is available over GitHub.
API Ref



No comments:

Post a Comment