Differences and Similarities between HashSet and CopyOnWriteArraySet with program in java


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



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


Differences between java.util.HashSet  and java.util.concurrent.CopyOnWriteArraySet in java >

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

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


As shown in Program 2 below.


3
Enumeration is fail-fast
Enumeration returned by HashSet is fail-fast, means any structural modification made to HashSet during iteration using Enumeration will throw ConcurrentModificationException.


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

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

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






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


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



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


Similarity between java.util.HashSet and java.util.concurrent.CopyOnWriteArraySet in java >

Property
java.util.HashSet and
java.util.concurrent.CopyOnWriteArraySet
1
Insertion order
HashSet and CopyOnWriteArraySet both does not maintains insertion order in java.
2
Allows null
HashSet and CopyOnWriteArraySet both allows to store one null in java.
3
Implements java.util.Set
HashSet and CopyOnWriteArraySet both are implementation of the java.util.Set interface and extends AbstractSet (Abstract class).
4
Structure and resizable
HashSet and CopyOnWriteArraySet both uses Map for their implementation.
Hence, structure is map based and resizing depends on Map implementation in java.
5
Duplicate elements
HashSet and CopyOnWriteArraySet both does not allow to store duplicate elements in java.



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


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


List vs Set in java - Similarity and Differences



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

TreeSet vs ConcurrentSkipListSet in java - Similarity and Differences with program


TreeMap vs ConcurrentSkipListMap in java - Similarity and Differences with program


No comments:

Post a Comment