What are main Implementations of Set interface

What are main Implementations of Set interface

Answer: Set implementations has been divided into two two parts:
general-purpose implemenation: In this group three general purpose Set implementations available i.e. TreeSet, HashSet and LinkedHashSet. TreeSet is slower than HashSet (factor: constant time versus log time for operations). In set implementation ordering of element is not guaranteed. Below is sample example which allocates HashSet intial capacity 32

Set<String> s = new HashSet<String>(32);


special-purpose implementation: In this group two special purpose Set implementations are avaiable which is : EnumSet and CopyOnWriteArraySet. On performace basis EnumSet is high performance Set implementation of enum types. Members defined in an enum set should be the same enum type. It is denoted through a bit-vector and typically would be single long. It also support iteration through ranges of enum types. It also provides rich, typesafe replacement for our traditional bit flags. Below is example:

EnumSet.of(Employee.NAME, Employee.AGE)


Leave a Reply

Your email address will not be published. Required fields are marked *