Map Interface

0
706

So far we have already covered the basics of Collection API and some of its interfaces. In this post, I am going to discuss about Map interface and the classes which implement it. Few of the classes are used widely specially the ones which are ordered or sorted.

Map Interface -unique identifiers (key)

Lets first understand the basic structure of Map as it confuses many people. The java.util.Map interface represents an object that has mapping between a key and a value. The Map interface is NOT a subtype of the Collection interface and that’s the reason it behaves a bit different from the rest of the collection types.

Map_Interface

We also need to note that a map can not contain duplicate keys as each key can map to at most one value. So the values can be duplicate but not the keys.
Map interface uses equals() to determine if 2 keys are same or different. Any class which implements Map must override hashCode() and equals() if you are interested in retrieving stuff from your map.

Now lets check the most important implementation classes of the map interface:

1) HashMap

HashMap is unsorted and unordered implementation of Map interface. You can use it when you don’t care about iteration order. It is based on key objects’ hash code (like HashSet) and allows 1 null key and multiple null values.

Constructors of HashMap:

HashMap() – Constructs an empty HashMap with the default initial capacity (16).
HashMap(int initialCapacity) – Constructs an empty HashMap with the specified initial capacity.
HashMap(int initialCapacity, float loadFactor) – Constructs an empty HashMap with the specified initial capacity and load factor.
HashMap(Map m) – Constructs a new HashMap with the same mappings as the specified Map.

2) Hashtable

Hashtable is among the first few most popular collection which is highly useful for storing the data and is Synchronized as well. You can also consider it as synchronized HashMap as well. A good big difference from HashMap though, is that it does not allow null keys or values.

Hashtable works by mapping key to the value, which is stored in an in-memory data structure. Rather than searching through all elements of the Hashtable for a given key, a hashing algorithm analyses the key, and returns an index number. This index matches the stored value, and the data is then accessed.

Hashtables accept as keys and values any Java object. We can use a String, for example, as a key, or perhaps a number such as an Integer. However, we can’t use a primitive data type for this purpose.

One point to note that, after Java 5 ConcurrentHashMap has almost taken over Hashtable and is preferable now over it. I will cover more about it later in another post.

Constructors for a Hashtable:

Hashtable() – Constructs a new, empty Hashtable with a default initial capacity (11) and load factor (0.75).
Hashtable(int initialCapacity) – Constructs a new, empty Hashtable with the specified initial capacity and default load factor (0.75).
Hashtable(int initialCapacity, float loadFactor) – Constructs a new, empty Hashtable with the specified initial capacity and the specified load factor.
Hashtable(Map t) – Constructs a new Hashtable with the same mappings as the given Map.

3) LinkedHashMap

As LinkedHashMap maintains the insertion order, it is an ordered map. It will iterate in the order in which the entries were put into the map. Though it is slower than HashMap for adding and removing elements but it provides faster Iteration.

Given below are the constructors for LinkedHashMap:
LinkedHashMap() – Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).
LinkedHashMap(int initialCapacity) – Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and a default load factor (0.75).
LinkedHashMap(int initialCapacity, float loadFactor) – Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor.
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) – Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.
LinkedHashMap(Map m) – Constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map.

4) TreeMap

TreeMap is a Sorted map imlementation. It lets you define custom sort order via Comparator or Comparable.
TreeMap extends SortedMap interface as well and is most useful when you are concerned with being able to sort or iterate over the keys in a particular order that you have defined.

Constructors of TreeMap:

TreeMap() – Constructs a new, empty tree map, using the natural ordering of its keys.
TreeMap(Comparator comparator) – Constructs a new, empty tree map, ordered according to the given comparator.
TreeMap(Map m) – Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys.
TreeMap(SortedMap m) – Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map.

Next I am going to talk about the Queue interface and few of its implementation classes.

Previous articleSet Interface
Next articleQueue Interface
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY