Difference between Set and Map

Both Set and Map are interfaces , Set is a sub interface to Collection interface. Map is an interface which is having key and value pairs . The main differences between Set and Map are given bellow with an example.


set and map Set interface is an unordered collection ,it won’t allow duplicate elements .
set and map It allows null values , not possible to get element (value)  by key .
set and map Set is not a synchronized collection , use Collections.synchronizedSet(new HashSet()) for synchronized set .


set and map Map interface is also an unordered collection ,but it allows duplicate elements.
set and map It allows null values , possible to get the value by its key .
set and map Map is not a synchronized collection , use Collections.synchronizedMap(new HashMap(…)) for synchronized Map


Methods in Set and Map  :

set and map set and map


Set setObject = new HashSet();
setObject.add()//add elements one by one
Map mapObject = new HashMap();
mapObject.put(key,value) //add elements like key,value

Set and Map example  :

Execution Result:


set and map  Set  collection can  traverse by Iterator interface, but Map cannot do it directly, so assign it to Collection.

set and map Use get(key) to get Map element , iterate with entrySet() method .

Related Posts :
What is the difference between interface and abstract class in java
What is Map interface in java
What is Set interface in java
How to iterate Map elements
Difference between List and Set


  1. maillot USA says:

    Very informative article post.Really thank you! Will read on

   [will not be published]

^ <