In this article, we will discuss Set interface in detail
1. Key points about Set:
- Set doesn’t allow duplicate items
- also doesn’t maintain insertion-order
2. Set interface:
- Set is a sub-interface of Collection interface (i.e.; Set extends Collection)
- To represent a group of element/objects as a single unit/entity, where duplicates aren’t allowed
- Set allows only unique element/objects to be inserted
- Set doesn’t maintain insertion-order
- While iterating through Set, we will get items in random-order (as against insertion-order in List)
- There are 2 direct concrete classes for Set interface viz.; HashSet, LinkedHashSet and another one TreeSet (through SortedSet –> NavigableSet)
- Set interface doesn’t contain any methods but only inherited methods from Collection interface
- Present in java.util package and extends java.util.Collection interface
Source: Team BenchResources.Net
3. Below listed classes implements Set interface
- HashSet
- LinkedHashSet
- TreeSet (through SoretdSet –> NavigableSet)
4. Factors to consider while discussing any collection class
We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,
- Underlying data structure
- Duplicates are allowed or Not
- Insertion order is maintained or Not
- Whether NULL insertion is possible or Not
- If possible, how many NULL values can be inserted
- Whether collection class provide sorting, by default
- Is there any way to apply customized sorting
- Performance, while dealing with retrieval or manipulation (addition/deletion)
- By default, all methods are synchronized or Not
Related Articles:
- Set interface
- HashSet class
- LinkedHashSet class
- SortedSet interface
- NavigableSet interface
- TreeSet class
- HashSet v/s LinkedHashSet
- HashSet v/s LinkedHashSet v/s TreeSet
- List v/s Set
- HashMap v/s HashSet
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
Happy Coding !!
Happy Learning !!