Hello fellow coders, I am back with my second article of our Java Collections Framework article series. So far we have covered all the classes that extend the Set interface and List interface.
If you can remember I wrote my first special article after covering the classes that extend the List interface. Since I have covered the classes that extend the Set interface, I am dedicating this special article to point out important points about the Set interface.
When it comes to the classes that extend the Set interface, these are the points you should keep in mind.
1. They do not keep track of the order
There is no way to track and get your elements after you add them. Basically do not use the classes that extend the Set interface, if you want to access the elements by index or something. You can go with a class that extends the List interface for that.
2. They do not allow duplicate values
This is a unique and important point that is specific to the Set interface. If your requirement is to have a list where you are sure that all the values are unique, a class that extends the Set interface is your best bet.
3. HashSet is faster than TreeSet
If you don’t intend to use value-ordered iteration, always go with a HashSet. LinkedHashSet is a class that is in an intermediate state between HashSet and TreeSet. LinkedHashSet provides insertion-ordered iteration and is faster than TreeSets.
4. Choose the initial capacity for HashSet mindfully
Read this explanation from Java Documentation.
One thing worth keeping in mind about
HashSetis that iteration is linear in the sum of the number of entries and the number of buckets (the capacity). Thus, choosing an initial capacity that’s too high can waste both space and time. On the other hand, choosing an initial capacity that’s too low wastes time by copying the data structure each time it’s forced to increase its capacity. If you don’t specify an initial capacity, the default is 16. In the past, there was some advantage to choosing a prime number as the initial capacity. This is no longer true. Internally, the capacity is always rounded up to a power of two. The initial capacity is specified by using theintconstructor.
Common Methods for Set Interface Implementations
1. boolean add (Element e)
Adding a given element to the set.
2. void clear()
Removes all the elements from the set
3. boolean contains(Object obj)
Checks if a given object is there in the set
4. boolean isEmpty()
This returns true if the set is empty
5. boolean remove(Object obj)
If the specified object is present in the list, this removes that element from the set and returns true. If the specified object is not there in the list, this returns false.
6. int size()
This returns the number of elements in the set
Thank you for reading my article. I hope you learned something valuable from it. If you did, drop a like and follow my blog to get notifications when I publish new articles. I write articles about coding, cricket and things that I find interesting. I try to publish articles every other day.
Have a nice day ✌