Thursday, July 23, 2020

Set Interface In Java

Set Interface In Java :
  • Set is an interface.
  • its belongs to java.util package and extends the Collection interface.
  • It is an unordered collection of objects.
  • Set is a collection of unique elements.It does not allow duplicate elements means no duplicate elements.
  • It allow only one null element.
public interface Set<E>
	extends Collection<E>
All Known Subinterfaces:
NavigableSet<E>, SortedSet<E>

All Known Implementing Classes:
AbstractSet, ConcurrentHashMap.KeySetView, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet
SortedSet Interface : 
public interface SortedSet<E>
	extends Set<E>

All Known Implementing Classes:
	ConcurrentSkipListSet, TreeSet

All Known Subinterfaces:
	NavigableSet<E> 
Implementation of Set
  • HashSet
  • LinkedHashSet
  • SortedSet : It is a sub-interface of set interface.
  • TreeSet : TheeSet is an implementation class of SortedSet. TreeSet has O(log(n)) time complexity for the add(), remove() and contains() operations because of the TreeMap implementation internally.
  • EnumSet
  • CopyOnWriteArraySet : CopyOnWriteArraySet is a thread-safe variant of HashSet, its uses a CopyOnWriteArrayList internally for all of its operations. The add(), remove() and contains() methods have O(n) average time complexity.
  • ConcurrentSkipListSet : ConcurrentSkipListSet have O(log(n)) time complexity for add(), remove() and contains(), as it's based in skip list data structure.
Time complexity For HashSet, LinkedHashSet, and EnumSet, the add(), remove() and contains() operations cost constant O(1) time. These are uses HashMap internally.

No comments:

Post a Comment