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:
- public ConcurrentSkipListSet(): Creates a new, empty set, ordered by natural sorting order.
- public ConcurrentSkipListSet(Collection<? extends E> c): Creates a new set containing the elements in the specified collection, ordered by natural sorting order.
- public ConcurrentSkipListSet(Comparator<? super E> comparator): Creates a new, empty set ordered by specified comparator.
- public ConcurrentSkipListSet(SortedSet<E> s): Creates a new set containing the same elements and using the same ordering as the specified sorted set.
Example:
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;
}
}
import java.util.concurrent.ConcurrentSkipListSet;
public class Test {
public static void main(String[] args) {
// 1. Using public ConcurrentSkipListSet()
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
ConcurrentSkipListSet
System.out.println(“Set2 is: ” + set2);
// 3. Using public ConcurrentSkipListSet(Comparator<? super E> comparator)
ConcurrentSkipListSet
set3.addAll(elements);
System.out.println(“Set3 is: ” + set3);
// 4. Using public ConcurrentSkipListSet(SortedSet
ConcurrentSkipListSet
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:
- boolean add(E e): Adds the specified element to this set if it is not already present.
- boolean addAll(Collection<? extends E> c): Adds all of the elements.
- void clear(): Removes all of the elements from this set.
- ConcurrentSkipListSet<E> clone(): Returns a shallow copy of this instance.
- 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.
- boolean contains(Object o): Returns true if this set contains the specified element.
- boolean isEmpty(): Returns true if this set contains no elements.
- int size(): This returns the number of elements in this set.
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;
}
}
import java.util.concurrent.ConcurrentSkipListSet;
public class Test {
public static void main(String[] args) {
List
ConcurrentSkipListSet
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
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
- Iterator<E> iterator(): Returns an iterator over the elements in this set in ascending order.
- Spliterator<E> spliterator(): Returns a Spliterator over the elements in this set.
- Iterator<E> descendingIterator(): Returns an iterator over the elements in this set in descending order.
- NavigableSet<E> descendingSet(): Returns a reverse order view of the elements contained in this set.
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);
}
}
import java.util.concurrent.ConcurrentSkipListSet;
public class Test {
public static void main(String[] args) {
List
ConcurrentSkipListSet
System.out.println(“set1 is : ” + set1);
// 9. Iterator iterator()
Iterator
System.out.print(“Value for itr1: “);
itr1.forEachRemaining(obj -> System.out.print(” ” + obj));
System.out.println();
// 10. Spliterator
Spliterator
System.out.print(“Value for spitr: “);
spitr.forEachRemaining(obj -> System.out.print(” ” + obj));
System.out.println();
// 11. Iterator descendingIterator()
Iterator
System.out.print(“Value for itr2: “);
itr2.forEachRemaining(obj -> System.out.print(” ” + obj));
System.out.println();
// 12. NavigableSet descendingSet()
NavigableSet
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]
- E first(): Returns the first (lowest) element currently in this set.
- E last(): Returns the last (highest) element currently in this set.
- 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.
- 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.
- 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.
- 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.
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));
}
}
import java.util.concurrent.ConcurrentSkipListSet;
public class Test {
public static void main(String[] args) {
List
ConcurrentSkipListSet
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
- E pollFirst(): Retrieves and removes the first (lowest) element, or returns null if this set is empty.
- E pollLast(): Retrieves and removes the last (highest) element, or returns null if this set is empty.
- boolean remove(Object o): Removes the specified element from this set if it is present.
- boolean removeAll(Collection<?> c): Removes from this set all of its elements that are contained in the specified collection.
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);
}
}
import java.util.concurrent.ConcurrentSkipListSet;
public class Test {
public static void main(String[] args) {
List
ConcurrentSkipListSet
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
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]
- NavigableSet<E> headSet(E toElement): Returns a view of the portion of this set whose elements are strictly less than toElement.
- 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.
- 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.
- 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.
- NavigableSet<E> tailSet(E fromElement): Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
- 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.
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));
}
}
import java.util.concurrent.ConcurrentSkipListSet;
public class Test {
public static void main(String[] args) {
List
ConcurrentSkipListSet
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]