java hashmap

Set / Map in Java

HashSet
  • HashSet class implements the Set interface
  • In HashSet, we store objects (elements or values) e.g. If we have a HashSet of string elements then it could depict a set of HashSet elements: {“One”, “Two”, “Three”, “Four”}
  • HashSet does not allow duplicate elements that mean you can not store duplicate values in HashSet.
  • HashSet permits to have a single null value.
  • HashSet is not synchronized which means they are not suitable for thread-safe operations until unless synchronized explicitly.
HashMap
  • HashMap class implements the Map interface
  • HashMap is used for storing key & value pairs. In short, it maintains the mapping of key & value (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This is how you could represent HashMap elements if it has integer key and value of String type: e.g. {1->”Monday”, 2->”Tuesday”, 3->”Wednesday”, 4->”Thursday”}
  • HashMap does not allow duplicate keys however it allows having duplicate values.
  • HashMap permits single null key and any number of null values.
  • HashMap is not synchronized which means they are not suitable for thread-safe operations until unless synchronized explicitly.
TreeSet / TreeMap

With TreeSets / TreeMaps, unlike HashSets / HasMaps, the elements are already arranged in a specific order.