Read Also : WebServices Interview Questions in Java
Difference between HashSet and CopyOnWriteArraySet in Java
1. Synchronization : HashSet is not synchronized . CopyOnWriteArraySet is synchronized. Synchronization means at a time only one thread can access the object.
2. Performance : HashSet is faster as it is not synchronized. That means many threads can execute the same piece of code simultaneously. In comparison , CopyOnWriteArraySet is slower.
3. Fail-fast vs Fail-safe : Iterators returned by HahSet's iterator and listiterator methods are fail-fast. CopyOnWriteArraySet uses fail-safe iterator . We have already shared the fail-fast vs fail-safe iterators in java with example
4. ConcurrentModificationException : HashSet can throw ConcurrentModificationException while CopyOnWriteArraySet can not .