The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial: Lambda expression in java | Java Lambda expressions[forEach method of Map]

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial: Lambda expression in java | Java Lambda expressions[forEach method of Map] Posted: May 29, 2017 10:50 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial: Lambda expression in java | Java Lambda expressions[forEach method of Map]
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=sAELCo4DxsI&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Lambda expression in java | Java Lambda expressions[forEach method of Map] 
LambdaDemo1.java
import java.util.LinkedHashMap;
import java.util.Map;

public class LambdaDemo1
{

public static void main(String[] args)
{

Map<Integer, String> map = new LinkedHashMap<>();
map.put(1, "Peter");
map.put(2, "John");
map.put(3, "Juli");
map.put(4, "Stephan");

/*
* Normal way to loop a Map.Before Java 8
*/

for (Map.Entry<Integer, String> entry : map.entrySet())
{
System.out.println("key : " + entry.getKey() + ", value : "
+ entry.getValue());
}

System.out.println("------------------------------------");

/*
* In Java 8, we can loop a Map with forEach + lambda expression.
*/

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

}

}
Output
key : 1, value : Peter
key : 2, value : John
key : 3, value : Juli
key : 4, value : Stephan
------------------------------------
key : 1 value : Peter
key : 2 value : John
key : 3 value : Juli
key : 4 value : Stephan
LambdaDemo2.java
import java.util.LinkedHashMap;
import java.util.Map;

public class LambdaDemo2
{

public static void main(String[] args)
{

Map<Integer, String> map = new LinkedHashMap<>();
map.put(1, "Peter");
map.put(2, "John");
map.put(3, "Juli");
map.put(4, "Stephan");

/*
* In Java 8, we can loop a Map with forEach + lambda
* expression+multiple statements.
*/


map.forEach((k, v) -> {
System.out.println("key : " + k + " value : " + v);
if ("John".equals(v))
{
System.out.println("Hello John");
}
});
}

}
Output
key : 1, value : Peter
key : 2, value : John
key : 3, value : Juli
key : 4, value : Stephan
------------------------------------
key : 1 value : Peter
key : 2 value : John
key : 3 value : Juli
key : 4 value : Stephan
Refer:
https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Map.html

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_map_foreach_app.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LambdaDemo_map_foreach_app

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4c13bd19ff03c6acdc5e87d7803290d625ce640a/BasicJava/LambdaDemo_map_foreach_app/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: Java Tutorial: Lambda expression in java | Java Lambda expressions[forEach method of Map]

    Topic: Lambda expression in java | Java Lambda expressions[Lambda expression example - Math Operation] Previous Topic   Next Topic Topic: Jdbc Example For Beginners

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use