Learnitweb

What is the difference between Set and Map?

Duplicate Objects

Set doesn’t allow duplicates.
Map doesn’t allow duplicate keys while it allows duplicate values.

Null elements

Set just allow one null element as there is no duplicate permitted while in Map you can have null values and at most one null key.

Order

Set doesn’t maintain any order. Few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order. Map also doesn’t store the elements in an order, however few of its classes, for e.g. TreeMap sorts the map in the ascending order of keys and LinkedHashMap sorts the elements in the insertion order.

Implementation classes

Set: AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet
Map: AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, PrinterStateReasons, Properties, Provider, RenderingHints, SimpleBindings, TabularDataSupport, TreeMap, UIDefaults, WeakHashMap

Use

If you want to create a collection of unique elements choose Set. If you store data in form of key and value then choose Map.