How to For Each in Hash Map in Java 8 using Lambda Expressions

Lambda Expression Java 8

In Java 1.8 (Java 8) this has become lot easier by using forEach method from Aggregate operations(Stream operations) that looks similar to iterators from Iterable Interface.

Just copy paste below statement to your code and rename the HashMap variable from hm to your HashMap variable to print out key-value pair.

HashMap<Integer,Integer> hm = new HashMap<Integer, Integer>();
/*
 *     Logic to put the Key,Value pair in your HashMap hm
 */

// Print the key value pair in one line.
hm.forEach((k,v) -> System.out.println("key: "+k+" value:"+v));

Below is the sample code that i tried using Lambda Expression. This stuff is so cool. Must try.

HashMap<Integer,Integer> hm = new HashMap<Integer, Integer>();

    Random rand = new Random(47);

    int i=0;
    while(i<100)
    {
        i++;
        int key = rand.nextInt(20);
        int value = rand.nextInt(50);
        System.out.println("Inserting key: "+key+" Value: "+value);
        Integer imap =hm.put(key,value);
        if( imap == null)
        {
            System.out.println("Inserted");
        }           
        else
        {
            System.out.println("Replaced with "+imap);
        }               
    }

    hm.forEach((k,v) -> System.out.println("key: "+k+" value:"+v));

Also one can use Spliterator for the same.

Spliterator sit = hm.entrySet().spliterator();

UPDATE


Including documentation links to Oracle Docs. For more on Lambda go to this link and must read Aggregate Operations and for Spliterator go to this link.

Advertisements

I am Computer Geek working in different Computer fields i.e. Designing, Developing, Programming, Debugging. Have patience to write, listen and read; these are the most basic traits for interacting socially with other human beings. Love to cook, go outing, painting, playing cricket/chess, listen to music. So all and all basic and to par advance skills for a highly intellectual minds. Laugh with me, share with me, enjoy listening to music with me, travel with me, have food with me. So all and all basic and to par advance skills for a highly interested and creative minds. Anyway lets get started this Blog and lets see what we do have in Future. Enjoy LIFE.

Tagged with: , , ,
Posted in Guide, Technical

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 496 other followers

September 2015
M T W T F S S
« Nov   Jan »
 123456
78910111213
14151617181920
21222324252627
282930  
Blog Stats
  • 1,487 hits
%d bloggers like this: