1.

Differentiate between Set and Map in Java.

Answer»

The Set interface is provided by the Java.util package. The set interface is established by extending the COLLECTION interface. We can't add the same element to it since it won't let US. Because it contains elements in a sorted order, it does not keep the insertion order. The Set interface in Java is used to build the MATHEMATICAL Set.

Map is SIMILAR to Set in that it is used to store a collection of objects as a single entity. A KEY-value pair is used to store each object. Because each value is associated with a unique key, we can quickly obtain the value using just the key.

SetMap
It cannot have values that are repeated. It is not possible to add the same elements to a set. Only the unique value is stored in each class that implements the Set interface.It is possible for different keys to have the same value. The map has a unique key and values that are repeated. 
Using the keyset() and entryset() methods, we can quickly iterate the Set items.It is not possible to iterate across map elements. To iterate the elements, we must convert Map to Set.
The Set interface does not keep track of insertion order. Some of its classes, such as LinkedHashSet, however, keep the insertion order. The Map does not keep track of the insertion sequence. Some Map classes, such as TreeMap and LinkedHashMap, do the same thing.


Discussion

No Comment Found