ConcurrentSkipListSet in Java

ConcurrentSkipListSet in java is also used to store elements in sorted order. Its implementation is based on the ConcurrentSkipListMap.

ConcurrentSkipListSet in java implements NavigableSet and extends AbstractSet. This is  thread safe hence multiple threads can access it at same time.

Following are the Constructors for ConcurrentSkipListSet in java:

  1. public ConcurrentSkipListSet(): Creates a new, empty set, ordered by natural sorting order.
  2. public ConcurrentSkipListSet(Collection<? extends E> c): Creates a new set containing the elements in the specified collection, ordered by natural sorting order.
  3. public ConcurrentSkipListSet(Comparator<? super E> comparator): Creates a new, empty set ordered by specified comparator.
  4. public ConcurrentSkipListSet(SortedSet<E> s): Creates a new set containing the same elements and using the same ordering as the specified sorted set.

Example:

Java Code
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

public class Test {
public static void main(String[] args) {
// 1. Using public ConcurrentSkipListSet()
ConcurrentSkipListSet set1 = new ConcurrentSkipListSet();
set1.add(23);
set1.add(45);
set1.add(12);
set1.add(89);
System.out.println(“Set1 is: ” + set1);

// 2. Using public ConcurrentSkipListSet(Collection<? extends E> c)
List elements = Arrays.asList(32, 12, 45, 2, 56, 32, 1, 22, 100, 432, 44, 65);
ConcurrentSkipListSet set2 = new ConcurrentSkipListSet(elements);
System.out.println(“Set2 is: ” + set2);

// 3. Using public ConcurrentSkipListSet(Comparator<? super E> comparator)
ConcurrentSkipListSet set3 = new ConcurrentSkipListSet(new MyComparator());
set3.addAll(elements);
System.out.println(“Set3 is: ” + set3);

// 4. Using public ConcurrentSkipListSet(SortedSet s)
ConcurrentSkipListSet set4 = new ConcurrentSkipListSet(set1);
System.out.println(“Set4 is: ” + set4);
}
}

// Comparator for reverse sorting order
class MyComparator implements Comparator<Object> {
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
if (i1 > i2)
return -1;
else if (i1 < i2)
return 1;
else
return 0;
}
}
Output:
Set1 is: [12, 23, 45, 89]
Set2 is: [1, 2, 12, 22, 32, 44, 45, 56, 65, 100, 432]
Set3 is: [432, 100, 65, 56, 45, 44, 32, 22, 12, 2, 1]
Set4 is: [12, 23, 45, 89]

Following methods are present in ConcurrentSkipListSet in java:

  1. boolean add(E e): Adds the specified element to this set if it is not already present.
  2. boolean addAll(Collection<? extends E> c): Adds all of the elements.
  3. void clear(): Removes all of the elements from this set.
  4. ConcurrentSkipListSet<E> clone(): Returns a shallow copy of this instance.
  5. 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.
  6. boolean contains(Object o): Returns true if this set contains the specified element.
  7. boolean isEmpty(): Returns true if this set contains no elements.
  8. int size(): This returns the number of elements in this set.
Java Code
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

public class Test {
public static void main(String[] args) {
List elements = Arrays.asList(32, 12, 45, 2, 56, 32, 1);
ConcurrentSkipListSet set1 = new ConcurrentSkipListSet (new MyComparator());
set1.add(45); // 1. boolean add(E e)
set1.addAll(elements); // 2. boolean addAll(Collection<? extends E> c)
System.out.println(“set1 is : ” + set1);

// 4. Object clone()
ConcurrentSkipListSet set2 = (ConcurrentSkipListSet ) set1.clone();
System.out.println(“set2 is : ” + set2);

set2.clear(); // 3. void clear()
System.out.println(“set2 after clear is :” + set2);
System.out.println(“Comarator : ” + set1.comparator()); // 5. Comparator<? super E> comparator()

System.out.println(“set1 contains(45): ” + set1.contains(45)); // 6. boolean contains(Object o)

// 7. boolean isEmpty()
System.out.println(“set1 isEmpty: ” + set1.isEmpty() + “, set2 isEmpty: ” + set2.isEmpty());

// 8. int size()
System.out.println(“set1 size: ” + set1.size() + “, set2 size: ” + set2.size());

}
}

// Comparator for reverse sorting order
class MyComparator implements Comparator<Object> {
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
if (i1 > i2)
return -1;
else if (i1 < i2)
return 1;
else
return 0;
}
}
Output:
set1 is : [56, 45, 32, 12, 2, 1]
set2 is : [56, 45, 32, 12, 2, 1]
set2 after clear is :[]
Comarator : MyComparator@64cee07
set1 contains(45): true
set1 isEmpty: false, set2 isEmpty: true
set1 size: 6, set2 size: 0

  1. Iterator<E> iterator(): Returns an iterator over the elements in this set in ascending order.
  2. Spliterator<E> spliterator(): Returns a Spliterator over the elements in this set.
  3. Iterator<E> descendingIterator(): Returns an iterator over the elements in this set in descending order.
  4. NavigableSet<E> descendingSet(): Returns a reverse order view of the elements contained in this set.
Java Code
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

public class Test {
public static void main(String[] args) {
List elements = Arrays.asList(32, 12, 45, 2, 56, 32, 1);
ConcurrentSkipListSet set1 = new ConcurrentSkipListSet(elements);

System.out.println(“set1 is : ” + set1);

// 9. Iterator iterator()
Iterator itr1 = set1.iterator();
System.out.print(“Value for itr1: “);
itr1.forEachRemaining(obj -> System.out.print(” ” + obj));
System.out.println();

// 10. Spliterator spliterator()
Spliterator spitr = set1.spliterator();
System.out.print(“Value for spitr: “);
spitr.forEachRemaining(obj -> System.out.print(” ” + obj));
System.out.println();

// 11. Iterator descendingIterator()
Iterator itr2 = set1.descendingIterator();
System.out.print(“Value for itr2: “);
itr2.forEachRemaining(obj -> System.out.print(” ” + obj));
System.out.println();

// 12. NavigableSet descendingSet()
NavigableSet nSet = set1.descendingSet();
System.out.println(“NavigableSet is : ” + nSet);
}
}
Output:
set1 is : [1, 2, 12, 32, 45, 56]
Value for itr1: 1 2 12 32 45 56
Value for spitr: 1 2 12 32 45 56
Value for itr2: 56 45 32 12 2 1
NavigableSet is : [56, 45, 32, 12, 2, 1]

  1. E first(): Returns the first (lowest) element currently in this set.
  2. E last(): Returns the last (highest) element currently in this set.
  3. E ceiling(E e): To return the least element in this set greater than or equal to the given element, or null if there is no such element.
  4. 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.
  5. 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.
  6. 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.
Java Code
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

public class Test {
public static void main(String[] args) {
List elements = Arrays.asList(32, 12, 45, 2, 56, 32, 1);
ConcurrentSkipListSet set1 = new ConcurrentSkipListSet(elements);
System.out.println(“set1 is : ” + set1);

// 13. E first()>>
System.out.println(“set1.first(): ” + set1.first());

// 14. E last()
System.out.println(“set1.last(): ” + set1.last());

// 15. E ceiling(E e)
System.out.println(“set1.ceiling(30): ” + set1.ceiling(30));
System.out.println(“set1.ceiling(32): ” + set1.ceiling(32));

// 16. E floor(E e)
System.out.println(“set1.floor(30): ” + set1.floor(30));
System.out.println(“set1.floor(32): ” + set1.floor(32));

// 17. E higher(E e)
System.out.println(“set1.higher(30): ” + set1.higher(30));
System.out.println(“set1.higher(32): ” + set1.higher(32));

// 18. E lower(E e)
System.out.println(“set1.lower(30): ” + set1.lower(30));
System.out.println(“set1.lower(32): ” + set1.lower(32));
}
}
Output:
set1 is : [1, 2, 12, 32, 45, 56]
set1.first(): 1
set1.last(): 56
set1.ceiling(30): 32
set1.ceiling(32): 32
set1.floor(30): 12
set1.floor(32): 32
set1.higher(30): 32
set1.higher(32): 45
set1.lower(30): 12
set1.lower(32): 12

  1. E pollFirst(): Retrieves and removes the first (lowest) element, or returns null if this set is empty.
  2. E pollLast(): Retrieves and removes the last (highest) element, or returns null if this set is empty.
  3. boolean remove(Object o): Removes the specified element from this set if it is present.
  4. boolean removeAll(Collection<?> c): Removes from this set all of its elements that are contained in the specified collection.
Java Code
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

public class Test {
public static void main(String[] args) {
List elements = Arrays.asList(32, 12, 45, 2, 56, 53, 67, 89, 32, 1);
ConcurrentSkipListSet set1 = new ConcurrentSkipListSet(elements);
System.out.println(“set1 is : ” + set1);

// 19. E pollFirst()
System.out.println(“set1.pollFirst(): ” + set1.pollFirst());
System.out.println(“set1 is : ” + set1);

// 20. E pollLast()
System.out.println(“set1.pollLast(): ” + set1.pollLast());
System.out.println(“set1 is : ” + set1);

// 21. boolean remove(Object o)
System.out.println(“set1.remove(12): ” + set1.remove(12));
System.out.println(“set1 is : ” + set1);

// 22. boolean removeAll(Collection<?> c)
List lst = Arrays.asList(45, 53, 56);
System.out.println(“set1.removeAll(45, 53, 56): ” + set1.removeAll(lst));
System.out.println(“final set1 is : ” + set1);
}
}
Output:
set1 is : [1, 2, 12, 32, 45, 53, 56, 67, 89]
set1.pollFirst(): 1
set1 is : [2, 12, 32, 45, 53, 56, 67, 89]
set1.pollLast(): 89
set1 is : [2, 12, 32, 45, 53, 56, 67]
set1.remove(12): true
set1 is : [2, 32, 45, 53, 56, 67]
set1.removeAll(45, 53, 56): true
final set1 is : [2, 32, 67]

  1. NavigableSet<E> headSet(E toElement): Returns a view of the portion of this set whose elements are strictly less than toElement.
  2. 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.
  3. 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.
  4. 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.
  5. NavigableSet<E> tailSet(E fromElement): Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
  6. 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.
Java Code
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

public class Test {
public static void main(String[] args) {
List elements = Arrays.asList(32, 12, 45, 2, 56, 32, 1);
ConcurrentSkipListSet set1 = new ConcurrentSkipListSet(elements);
System.out.println(“set1 is : ” + set1);

// 23. SortedSet headSet(E toElement)
System.out.println(“set1.headSet(30): ” + set1.headSet(30));
System.out.println(“set1.headSet(32): ” + set1.headSet(32));

// 24. NavigableSet headSet(E toElement, boolean inclusive)
System.out.println(“set1.headSet(32, false): ” + set1.headSet(32, false));
System.out.println(“set1.headSet(32, true): ” + set1.headSet(32, true));

// 25. NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
System.out.println(“set1.subSet(1, false, 32, false): ” + set1.subSet(1, false, 32, false));
System.out.println(“set1.subSet(1, true, 32, true): ” + set1.subSet(1, true, 32, true));

// 26. SortedSet subSet(E fromElement, E toElement)
System.out.println(“set1.subSet(10, 56): ” + set1.subSet(10, 56));
System.out.println(“set1.subSet(10, 60): ” + set1.subSet(10, 60));

// 27. SortedSet tailSet(E fromElement)
System.out.println(“set1.tailSet(10): ” + set1.tailSet(10));
System.out.println(“set1.tailSet(12): ” + set1.tailSet(12));

// 28. NavigableSet tailSet(E fromElement, boolean inclusive)
System.out.println(“set1.tailSet(12, false): ” + set1.tailSet(12, false));
System.out.println(“set1.tailSet(12, true): ” + set1.tailSet(12, true));
}
}
Output:
set1 is : [1, 2, 12, 32, 45, 56]
set1.headSet(30): [1, 2, 12]
set1.headSet(32): [1, 2, 12]
set1.headSet(32, false): [1, 2, 12]
set1.headSet(32, true): [1, 2, 12, 32]
set1.subSet(1, false, 32, false): [2, 12]
set1.subSet(1, true, 32, true): [1, 2, 12, 32]
set1.subSet(10, 56): [12, 32, 45]
set1.subSet(10, 60): [12, 32, 45, 56]
set1.tailSet(10): [12, 32, 45, 56]
set1.tailSet(12): [12, 32, 45, 56]
set1.tailSet(12, false): [32, 45, 56]
set1.tailSet(12, true): [12, 32, 45, 56]

-A blog by Shwetali Khambe

Related Posts