Friday, 25 December 2015

Dude Vs Super Dude: Episode 1 


Dude: I wanted  to buy a PencilBox and CompassBox and put some stationery in it. I took two ArrayList and put the items into it. But am confused how I am going to place corresponding stationery into each box.

               List box =new ArrayList();
                       box.add("PencilBox");
                       box.add("CompassBox");
              List stationery =new ArrayList();
                      stationery.add("Pencil");
                      stationery.add("Eraser");
                      stationery.add("Sharpener");
                      stationery.add("Protector");
                      stationery.add("Compass");
                      System.out.println(box);
                      System.out.println(stationery);

Super Dude: Hey Buddy, with ArrayList you can only place group of different objects in an order and  you can sort them if required. But to map two different items, you are in a very wrong place. Don’t Worry! HashMap is available in the market under java.util package, come lets buy some.

Dude: Why buy a HashMap?

Super Dude: Because it implemented the legendary Map<K,V> interface. So Cool huh??

Dude: What’s so cool in it?

Super Dude: Map is the feature of Collection framework which will help us map one item to another which is termed as a Key(K) and Value(V) pair.

Dude: Does it help me to map my PencilBox and stationery?

Super Dude: Sure it does more than that. A map cannot contain duplicate keys so that you can uniquely identify the value using the key.Each key can map to at most one value.  The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys(Only Keys), collection of values(Only Value), or set of key-value mappings(K-V pair), served to you in whatever manner you want. 

Dude: Wow! Awwesome!!

Super Dude: Let me show you how you can do that with a small example,

package com.awf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HashMapDemo {

    public static void main(String[] args) {
        List<String> box = new ArrayList();    //Here is your box list
        box.add("PencilBox");
        box.add("CompassBox");
        List pencilBoxStationery = new ArrayList(); //Here is your pencil-box stationery 
        pencilBoxStationery.add("Pencil");
        pencilBoxStationery.add("Eraser");
        pencilBoxStationery.add("Sharpener");
        List compassBoxStationery = new ArrayList(); //Here is your compass-box stationery
        compassBoxStationery.add("Protector");
        compassBoxStationery.add("Compass");

// HashMap implementation
        Map<String, List> objHashMap = new HashMap<String, List>(); 

//This is where we are mapping box list with the corresponding stationery list
        for (String boxes : box) {
            if ("PencilBox".equals(boxes)) {
                objHashMap.put(boxes, pencilBoxStationery);// Maps PencilBox with stationery
            } else {
                objHashMap.put(boxes, compassBoxStationery);// Maps CompassBox with stationery
            }
        }
        System.out.println(objHashMap);
    }

}

Output will be :
{PencilBox=[Pencil, Eraser, Sharpener], CompassBox=[Protector, Compass]}

You can see that the pencil box stationery are mapped within PencilBox and compass box stationery are mapped with the CompassBox within a HashMap object.

Dude: This is the coolest stuff I have ever seen..!! Now i will map all cities with district and all district with states and all states with country and all countries with the world.. Hahaha!!!

Super Dude [Did he really learned map of maps already..??? ]
                                                                     
                                                                                -Will be Continued...

#jAvaLoVEr                                                                                                               AWF

No comments:

Post a Comment