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
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.
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.
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.
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.
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.
Leave a Reply