Java-Map interface - hierarchy-examples

java.util
Interface Map<K,V>

Type Parameters:

K - the type of keys maintained by this map

V - the type of mapped values




An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Java Map Hierarchy





Map :The Map Interface. A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. 

HashMap:HashMap is the implementation of Map, but it doesn't maintain any order.It stores the data in (Key, Value) pairs. To access a value one must know its key. HashMap is known as HashMap because it uses a technique called Hashing. Hashing is a technique of converting a large String to small String that represents the same String. A shorter value helps in indexing and faster searches. HashSet also uses HashMap internally.

HashTable: HashTable is the implementation for Map.Hashtable is an array of a list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.

LinkedHashMap: Java LinkedHashMap class is Hashtable and Linked list implementation of the Map interface, with predictable iteration order. It inherits HashMap class and implements the Map interface.

SortedMap:SortedMap is an interface in collection framework. This interface extends Map inrerface and provides a total ordering of its elements (elements can be traversed in sorted order of keys)

NavigableMap:NavigableMap is an extension of SortedMap which provides convenient navigation method like lowerKey, floorKey, ceilingKey and higherKey, and along with these popular navigation method it also provide ways to create a Sub Map from existing Map in Java e.g. headMap whose keys are less than specified key, tailMap whose keys are greater than specified key and a subMap which is strictly contains keys which falls between toKey and fromKey.

TreeMap:Java TreeMap class is a red-black tree based implementation. It provides an efficient means of storing key-value pairs in sorted order.

Map methods and Descriptions

void clear( ):Removes all key/value pairs from the invoking map.


boolean containsKey(Object k):Returns true if the invoking map contains k as a key. Otherwise, returns false.


boolean containsValue(Object v):Returns true if the map contains v as a value. Otherwise, returns false.


Set entrySet( ):Returns 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.


boolean equals(Object obj):Returns true if obj is a Map and contains the same entries. Otherwise, returns false.


Object get(Object k):Returns the value associated with the key k.


int hashCode( ):Returns the hash code for the invoking map.


boolean isEmpty( ):Returns true if the invoking map is empty. Otherwise, returns false.


Set keySet( ):Returns a Set that contains the keys in the invoking map. This method provides a set-view of the keys in the invoking map.


Object put(Object k, Object v):Puts an entry in the invoking map, overwriting any previous value associated with the key. The key and value are k and v, respectively. Returns null if the key did not already exist. Otherwise, the previous value linked to the key is returned.


void putAll(Map m):Puts all the entries from m into this map.


Object remove(Object k):Removes the entry whose key equals k.


int size( ):Returns the number of key/value pairs in the map.


Collection values( ):Returns a collection containing the values in the map. This method provides a collection-view of the values in the map.



Example:

Map has its implementation in various classes like HashMap. Following is an example to explain map functionality −


import java.util.HashMap;
import java.util.Map;

public class MapKnowledgefactoryDemo {


public static void main(String[] args) {

Map<Integer, String> m1 = new HashMap<>();
// Puts an entry in the invoking map
m1.put(1, "Sibin");
// Puts an entry in the invoking map
m1.put(2, "Rahul");
// Puts an entry in the invoking map
m1.put(3, "Scott");
// Puts an entry in the invoking map
m1.put(4, "Julious");

// Printing map elements

System.out.print("Map Elements:");
System.out.println("\t" + m1);

// Returns the value associated with the key k :m1.get(1)

System.out.print("Returns the value associated with the key 1: ");
System.out.println(m1.get(1));

// Returns the number of key/value pairs in the map :m1.size().

System.out.print("Returns the number of key/value pairs in the map: ");
System.out.println(m1.size());

// Returns true if the invoking map contains k as a key. Otherwise, returns

// false:containsKey(2)
System.out.print("Returns true if the invoking map contains k as a key: ");
System.out.println(m1.containsKey(1));

// Returns true if the map contains v as a value. Otherwise, returns

// false:containsValue(sibin)
System.out.print("Returns true if the map contains v as a value: ");
System.out.println(m1.containsValue("Sibin"));

}

}

Output:

Map Elements: {1=Sibin, 2=Rahul, 3=Scott, 4=Julious}
Returns the value associated with the key 1: Sibin
Returns the number of key/value pairs in the map: 4
Returns true if the invoking map contains k as a key: true
Returns true if the map contains v as a value: true



This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments