What is the Difference Between Set and Map

The main difference between Set and Map is that Set is a subtype of collection interface while Map is a not a subtype of collection interface.

An array stores the data of the same type. But, after creating an array, it is not possible to change it. Programming languages such as Java provides collections for storing data dynamically. It allows storing multiple objects as a single group. Moreover, it is possible to perform operations such as sorting, searching, inserting, and deleting on a collection. The base interface to implement collections in Java is called Collection. There are various interfaces that extend the Collection interface. One of them is Set interface. On the other hand, Map does not extend the Collection interface as it is a separate interface.

Key Areas Covered

1. What is Set
     – Definition, Functionality
2. What is Map
     – Definition, Functionality
3. What is the Difference Between Set and Map
     – Comparison of Key Differences

Key Terms

Java, Map, Set

Difference Between Set and Map - Comparison Summary

What is Set

Set is an interface that extends the Collection interface. It cannot contain duplicate values. HashSet and TreeSet implement the Set. The Set interface provides methods such as add, size etc. to perform operations on the collection. An example is as follows.

Difference Between Set and Map

Figure 1: Program with HashSet

The HashSet is an object of type Set. It can store Strings. The add method is used to insert elements to HashSet.  Even though there are two strings as “apple”, there is only one apple in the output. Therefore, it does not display the elements that already exist in the Set. Furthermore, it prints the Set as an unordered list. The size method gives the number of items in the Set.

Refer another example as follows.

Difference Between Set and Map_Figure 2

Figure 2: Program with TreeSet

The treeset is an object of type Set which can store Strings. The add method is used to insert elements to treeset.  Even though there are two strings as “apple”, there is only one apple in the output. Therefore, it only displays the unique elements. Furthermore, it prints the Set as an ordered list.  The size method gives the number of items in the Set.

What is Map

Map is an interface that represents a mapping between a key and a value. It does not extend the Collection interface. Therefore, the behavior of Map is different from the usual Collection types. A Map cannot contain duplicate keys, and each key can map to at most one value. HashMap implements Map interface while SortedMap extends the Map interface and TreeMap implements the SortedMap interface.

Main Difference -  Set vs  Map

Figure 3: Program with HashMap

The studentList is an object of type Map. It can store Strings. The put method is used to insert elements to studentList. There are two records on “Peter” but it will only display one record. HashMap does not maintain the inserted order. It only prints the unique values.

Difference Between Set and Map_Figure 4

Figure 4: Program with TreeMap

Letters is an object of type Map. It can store Strings. The put method is used to insert elements to letters. There are two records on “B”, but it will only display one record. TreeMap does not maintain the inserted order. It only prints the unique values.

Difference Between Set and Map

Definition

A Set is an interface in Collection hierarchy that cannot contain duplicate elements whereas a Map is an interface that maps unique keys to values. This is the main difference between Set and Map.

Association with Collection Interface

Set is an interface that extends Collection interface while Map is a separate interface.

Functionality

Functionality is also a major difference between Set and Map. Set helps to store unique values whereas Map interface represents a mapping between a key and a value.

Conclusion

The main difference between Set and Map is that Set is a subtype of Collection interface while Map is a not a Subtype of Collection interface.

Reference:

1. “Set in Java.” GeeksforGeeks, 26 Nov. 2018, Available here.
2. “Map Interface in Java.” GeeksforGeeks, 26 Nov. 2018, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply