Differences and Similarities between TreeSet and ConcurrentSkipListSet with program in java


It’s very important to differentiate between java.util.TreeSet and java.util.concurrent.ConcurrentSkipListSet, so in this Collection framework tutorial we will learn what are differences and similarities between java.util.TreeSet and java.util.concurrent.ConcurrentSkipListSet in java.




Contents of page :
  • Differences between java.util.TreeSet and java.util.concurrent.ConcurrentSkipListSet in java >
  • Similarity between java.util.TreeSet and java.util.concurrent.ConcurrentSkipListSet in java >
  • Program 1 to show
    • Iterator and Enumeration returned by TreeSet are Fail-fast in java
  • Program 2 to show
    • Iterator and Enumeration returned by ConcurrentSkipListSet are Fail-safe in java.


Differences between java.util.TreeSet  and java.util.concurrent.ConcurrentSkipListSet in java >

Property
java.util.TreeSet
java.util.concurrent. ConcurrentSkipListSet
1
synchronization
TreeSet is not synchronized  (because 2 threads on same TreeSet object can access it at same time) in java.
ConcurrentSkipListSet  is synchronized  (because 2 threads on same ConcurrentSkipListSet object cannot access it at same time) in java.
2
Iterator
Iterator returned by TreeSet is Fail-fast, means any structural modification made to TreeSet during iteration using Iterator will throw ConcurrentModificationException in java.

As shown in Program 1 below.
Iterator returned by ConcurrentSkipListSet is Fail-safe in java.





As shown in Program 2 below.
3
Enumeration is fail-fast
Enumeration returned by TreeSet is fail-fast, means any structural modification made to TreeSet during iteration using Enumeration will throw ConcurrentModificationException.


As shown in Program 1 below.
Enumeration returned by ConcurrentSkipListSet is fail-safe.







As shown in Program 2 below.
4
Iterate using enhanced for loop
Iteration done on TreeSet using enhanced for loop is Fail-fast, means any structural modification made to TreeSet during iteration using enhanced for loop will throw ConcurrentModificationException.

As shown in Program 1 below.
Iteration done on ConcurrentSkipListSet using enhanced for loop is Fail-safe.

As shown in Program 2 below.
5
Performance
TreeSet is not synchronized, hence its operations are faster as compared to ConcurrentSkipListSet.
ConcurrentSkipListSet is synchronized, hence its operations are slower as compared to TreeSet.
6
Introduced in which java version
TreeSet was introduced in second version of java (1.2) i.e. JDK 2.0
ConcurrentSkipListSet was introduced in sixth version of java (1.6) i.e. JDK 6.0
7
Package
java.util
java.util.concurrent


So far we have learned what are differences between java.util.TreeSet and java.util.concurrent.ConcurrentSkipListSet in java.
Now we will learn similarities in java.util.TreeSet and java.util.concurrent.ConcurrentSkipListSet in Collection framework in java.




/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


Similarity between java.util.TreeSet and java.util.concurrent.ConcurrentSkipListSet in java >

Property
java.util.TreeSet and
java.util.concurrent.ConcurrentSkipListSet
1
Insertion order
TreeSet and ConcurrentSkipListSet both are sorted by natural order.

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

Output >     
a
b
c
2
Allows null
TreeSet and ConcurrentSkipListSet both does not allows to store null.

Any attempt to add null throws runtimeException (NullPointerException).
3
Implements java.util.Set
TreeSet and ConcurrentSkipListSet both are implementation of the java.util.Set, SortedSet
NavigableSet interface and extends AbstractSet (Abstract class).
4
Structure and resizable
TreeSet and ConcurrentSkipListSet both uses Map for their implementation.
Hence, structure is map based and resizing depends on Map implementation.
5
Duplicate elements
TreeSet and ConcurrentSkipListSet both does not allow to store duplicate elements.




Example/Program 1 to show
Iterator and Enumeration  returned by TreeSet are Fail-fast, means any structural modification made to TreeSet during iteration will throw ConcurrentModificationException in java.
Also, iteration done on TreeSet  using enhanced for loop is Fail-fast in java.
import java.util.Collections;
import java.util.Enumeration;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Set;
public class TreeSetExample {
public static void main(String args[]){
  
   Set<String> treeSet=new TreeSet<String>();
   treeSet.add("audi");
   //fail-fast
   Iterator<String> iterator=treeSet.iterator();
   while(iterator.hasNext()){
          treeSet.add("newElement1"); //unComment to avoid ConcurrentModificationException
          System.out.println(iterator.next());
   }
  
  
   //fail-fast
   Enumeration<String> listEnum=Collections.enumeration(treeSet);
   while(listEnum.hasMoreElements()){
      treeSet.add("newElement2"); //unComment to avoid ConcurrentModificationException
      System.out.println(listEnum.nextElement());
   }
   //enhanced for loop is fail-fast
   for(String string:treeSet){
          treeSet.add("newElement3"); //unComment to avoid ConcurrentModificationException
          System.out.println(string);
   }
  
}
}
In above program structural modification was made to TreeSet during iteration, and that didn’t throwed ConcurrentModificationException.


Example/Program 2 to show
Iterator and Enumeration  returned by ConcurrentSkipListSet are Fail-safe, means any structural modification made to ConcurrentSkipListSet during iteration does not throw any exception.
Also, iteration done on ConcurrentSkipListSet using enhanced for loop is Fail-safe in java.
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
public class ConcurrentSkipListSetExample {
public static void main(String args[]){
  
   Set<String> concurrentSkipListSet=new ConcurrentSkipListSet<String>();
   concurrentSkipListSet.add("audi");
  
   //fail-safe
   Iterator<String> iterator=concurrentSkipListSet.iterator();
   while(iterator.hasNext()){
          concurrentSkipListSet.add("newElement1");
          System.out.println(iterator.next());
   }
  
   //fail-safe
   Enumeration<String> listEnum=Collections.enumeration(concurrentSkipListSet);
   while(listEnum.hasMoreElements()){
      concurrentSkipListSet.add("newElement2");
      System.out.println(listEnum.nextElement());
   }
   //enhanced for loop is fail-safe
   for(String string:concurrentSkipListSet){
          concurrentSkipListSet.add("newElement3");
          System.out.println(string);
   }
  
  
  
}
}
In above program structural modification was made to ConcurrentSkipListSet during iteration, but that didn’t throw ConcurrentModificationException in java.


So in this Collection framework tutorial we learned what are differences and similarities between java.util.TreeSet and java.util.concurrent.ConcurrentSkipListSet in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Collection - List, Set and Map all properties in tabular form in java


Set hierarchy tutorial in java - Detailed - java.util.HashSet, java.util.concurrent.CopyOnWriteArraySet, java.util.LinkedHashSet, java.util.TreeSet, java.util.concurrent.ConcurrentSkipListSet, java.util.EnumSet classes


Important Similarity and Differences in java >

List Differences in java >

ArrayList vs LinkedList - Similarity and Differences in java


ArrayList vs Vector - Similarity and Differences in java


List vs Set - Similarity and Differences in java


Important Similarity and Differences Collection classes in concurrent and non-concurrent packages >

HashSet vs CopyOnWriteArraySet in java - Similarity and Differences with program

TreeMap vs ConcurrentSkipListMap in java - Similarity and Differences with program


No comments:

Post a Comment