Javatpoint Logo
Javatpoint Logo

Difference Between Set and Map in Java

In Java, Set and Map are two important interfaces available in the collection framework. Both Set and Map interfaces are used to store a collection of objects as a single unit. The main difference between Set and Map is that Set is unordered and contains different elements, whereas Map contains the data in the key-value pair.

In this section, we will discuss the Set and Map interface in detail also see the differences between them.

Set Interface

The Java.util package provides the Set interface. Set is implemented by extending the collection interface. It doesn't allow us to add the same element to it. It doesn't maintain the insertion order because it contains elements in a sorted way. For designing the mathematical Set, we use the Set interface in Java.

Set vs Map in Java

Let's implement the Set interface in a Java program.

SetExample.java

Output:

Set vs Map in Java

Map Interface

Map is another important interface available in the Collection interface. So, in order to use the Map interface, we have to extend the Collection interface. Just like Set, Map is also used for storing collection of objects as a single unit. Each object is store in a key-value pair. We can easily access the value using just the key because each value is associated with a unique value.

We can easily search, update or delete the element by using the Map. Map and SortedMap are the two interfaces, and TreeMap, LinkedHashMap, and HashMap are three classes of Map.

If we need to traverse a Map, we have to convert it into Set because Map cannot be traversed. After converting it into Set, we can traverse it using the Set methods like keySet() and entrySet().

Set vs Map in Java

Let's understand how we can create a Map in Java by taking an example.

MapExample.java

Output:

Set vs Map in Java

Difference Between Set and Map Interfaces

Both interfaces are used to store the collection of objects as a single unit. The main difference between Set and Map is that Set contains only data elements, and the Map contains the data in the key-value pair, so Map contains key and its value.

Now, let's understand some major differences between both of them.

S.No. Set Map
1. Set is used to construct the mathematical Set in Java. Map is used to do mapping in the database.
2. It cannot contain repeated values. It can have the same value for different keys.
3. Set doesn't allow us to add the same elements in it. Each class that implements the Set interface contains only the unique value. Map contains unique key and repeated values. In Map, one or more keys can have the same values, but two keys cannot be the same.
4. We can easily iterate the Set elements using the keyset() and the entryset() method of it. Map elements cannot be iterated. We need to convert Map into Set for iterating the elements.
5. Insertion order is not maintained by the Set interface. However, some of its classes, like LinkedHashSet, maintains the insertion order. The insertion order is also not maintained by the Map. However, some of the Map classes like TreeMap and LinkedHashMap does the same.





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA