A Set in Java is an interface which forms an important pillar of the Collections framework. It extends the Collection interface and hence has all the basic methods of a Collection. The unique thing about Set is it stores only unique values, no duplicates. Also it does not have indexes and hence it does not preserve the insertion order (except Linke...

Read More

The most important methods in a Set are boolean add(E e); boolean remove(Object o); int size(); Iterator iterator(); boolean contains(Object o); add(), remove(), size() and contains() make sense but why is iterator() an important method? Simply because Iterator is the only straightforward way to iterating over a Set since we do not have index...

Read More

You would need to just think about what type of Objects will you be storing in this Set. If you are going to use any of the Java provided types like String, Integer, Double etc then you don't need to think any further, just go ahead and start collecting objects in your Set object. If you are thinking of using a custom class as a type then you need ...

Read More

A Set should not contain duplicate elements. That is one of the major reasons to use a set. There are 3 implementations of Set HashSet TreeSet LinkedHashSet Let us understand how to choose the specific Set implementation based on your requirement. In brief : If you need a fast set, you should use HashSet. If you need a sorted set, then use TreeSet....

Read More

In this tutorial we will see how java.util.HashSet works internally. It is very common Collection framework interview question. Some interviewer can even ask you to implement your own Set interface which behaves similar like a HashSet. In both cases you need to know internal working of HashSet or How HashSet works in Java. How will you make sure ea...

Read More

FOLLOW US ON LinkedIn



Explore Tutu'rself