Sunday, 21 February 2016

Java Collections - Map Interface



Java Collection – MAP

  •          It is an Interface
  •          It stores values in  key-value pair
  •          Each key-value is known as ENTRY
  •          Map is an Object that maps key to Value
  • ·     It does not contain duplicate Key and each key can map to at most one value
For Understanding, We can say that, Map is a combination of set and list



Let’s investigate the above statement which is said above.
  •          SET only allows unique element as same as MAP’s key allow unique element
  •          LIST allows duplicate elements as same as MAP’s values also allow the duplicate element. 


Map interface include methods 

Operations
Method Name
Basic Operations
put, get, remove, containsKey, containsValue, size, and empty
Bulk Operations
putall and clear
collection Operations
KeySet,entryset and values
 

Analysis MAP declaration and its implemented class

I am using java 6 for below example:


  1. Map <Integer,String> mySampleMap = new HashMap <Integer,String> ();
  2. System.out.println("1");

I am using HashMap implementation for Map interface, which accept Integer value as Key and String value as Map value.

When debug point comes to second line (2.) and I inspect the map reference (mySampleMap).
 

 

Now I am adding a value in map and let see what happen in map reference.



              3.   mySampleMap.put(1, "One");
 



Let understand the Map reference properties



     EntrySet
As we know that, Entry is KEY-VALUE pair. Set of the collection view is called EntrySet. entrySet is the method of MAP interface which returns a collection view of the map.
 

 package com.sample.collections;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class EntrySetMapExample {

    public static void main(String[] args) {

        Map<Integer,String> hashMap = new HashMap<Integer,String>();
      
        hashMap.put(1, "hello");
        hashMap.put(2, "hello world");
      
        Set<Entry<Integer,String>> setEntry = hashMap.entrySet();
      
        for(Entry<Integer,String> setEntryObj : setEntry) {
            System.out.println("Key ::::---> "+setEntryObj.getKey()+" and  Value ::::---> "+setEntryObj.getValue());
        }

    }
}



Output:

Key ::::---> 1 and Value ::::---> hello
Key ::::---> 2 and Value ::::---> hello world



     KeySet

It returns the set of keys

public Set<K> keySet()

Example:
Set<Integer> setKeys = hashMap.keySet();
                               
                                for(Integer key : setKeys) {
                                                System.out.println("Key "+key);
                                }



Map implementations are grouped in three part as mentioned below. (Each map implementation has their own features, we will cover it in next post /article.)

Group Name
Map Implementation
General-Purpose Map
HashMap , TreeMap and LinkedHashMap
Special-Purpose Map
EnumMap, WeakHashMap and IdentityHashMap
Concurrent Map
ConcurrentHashMap
 

You may also like to read below article related to MAP  interface.

http://www.jigarnagar.com/2015/07/hash-map-initial-capacity-and-load.html

No comments:

Post a Comment