java map examples

By | September 5, 2019

Difference between HashMap, LinkedHashMap, TreeMap and Hashtable with examples:

HashMap LinkedHashMap TreeMap HashTable
Insertion Order No Yes Yes No
Null key allowed allowed Not allowed Not allowed
Null value allowed allowed allowed Not allowed

Examples:

 

Answer:

==================

Duplicate keys are not allowed in map and each key can map to at most one value.

  • HashMap and LinkedHashMap allow null key and null value,
  • TreeMap doesn’t allow null key,
  • Hashtable doesn’t allow null key and null values.

HashMap and Hashtable have no predictable order but LinkedHashMap and TreeMap have predictable order.

HashMap: HashMap implements all of the Map operations and allows null values and one null key. HashMap does not maintain an order of its key-value elements. Consider using HashMap when order does not matter and nulls are acceptable.

 

LinkedHashMap: LinkedHashMap is the implementation of Map and it inherits HashMap class. It allows null keys and values. LinkedHashMap maintains insertion order. Consider using a LinkedHashMap when you want a map with its key-value pairs are sorted by their insertion order.

TreeMap: TreeMap is the implementation of Map and SortedMap. It maintains ascending order. It doesn’t allow nulls. So consider using a TreeMap when you want a Map sorts its key-value pairs by the natural order of the keys.

 

Summary points:

  • Map doesn’t allow duplicate keys, but it allows duplicate values.
  • HashMap and LinkedHashMap allows null keys and null values but TreeMap doesn’t allow any null key or null value.
  • Map can’t be traversed so we need to convert it into Set using keySet() or entrySet() method.

The Map interface includes methods for basic operations such as (put, get, remove, containsKey, containsValue, size, and empty), bulk operations (such as putAll and clear), and collection views such as (keySet, entrySet, and values).

Methods in Map Interface:

  • public Object put(Object key, Object value): This method is used to insert an entry in the map.
  • public void putAll(Map m): This method is used to insert specified map in this map.
  • public Object remove(Object k): This method is used to delete an entry whose key equals k.
  • public Object get(Object k):This method is used to return the value associated with the key k.
  • public boolean containsKey(Object k): This method returns true if the invoking map contains k as key. Otherwise returns false.
  • public Set keySet(): This method is used to return a Set that contains the keys in the invoking map. This method provides a set view of the keys in the invoking map.
  • public Set entrySet(): This method is used to return a Set that contains the entries in the map. The set contains objects of type Map.Entry. This method provides a set-view of the invoking map.

Collection Views

The collection view methods allow a Map to be viewed as a Collection in the following ways.

  1. keySet – the Set of keys contained in the Map
  2. values – Collection of values contained in the Map. This Collection is not a Set, because multiple keys can map to the same value
  3. entrySet – the Set of key-value pairs contained in the Map. The Map interface provides a small nested interface called Map. Entry, the type of the elements in this Set.

Leave a Reply

Your email address will not be published. Required fields are marked *