Java 8 Api Continue in Foreach
In this article, we will see "How to iterate a Map and a List using forEach statement in Java 8" . Here, we will go through several examples to understand this feature.
Iterate Map & List using Java 8 forEach...!!! Click To Tweet
Example 1. Map with forEach
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java . util . HashMap ; import java . util . Map ; import java . util . function . BiConsumer ; public class ForEach_Java8_Map_Example { public static void main ( String [ ] args ) { Map < Integer , String > map = new HashMap < Integer , String > ( ) ; map . put ( 65 , "noni" ) ; map . put ( 33 , "yogi" ) ; map . put ( 73 , "dv" ) ; map . put ( 46 , "pulkit" ) ; map . put ( 37 , "isha" ) ; map . put ( 98 , "yashi" ) ; map . put ( 54 , "yashu" ) ; System . out . println ( "forEach Anonymous Class Implementation:\n" ) ; map . forEach ( new BiConsumer < Integer , String > ( ) { @Override public void accept ( Integer k , String v ) { System . out . println ( "Key: " + k + " Value: " + v ) ; } } ) ; System . out . println ( "\nforEach Lambda Implementation:\n" ) ; map . forEach ( ( k , v ) - > System . out . println ( "Key: " + k + " Value: " + v ) ) ; } } |
Output:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | forEach Anonymous Class Implementation : Key : 65 Value : noni Key : 33 Value : yogi Key : 98 Value : yashi Key : 37 Value : isha Key : 54 Value : yashu Key : 73 Value : dv Key : 46 Value : pulkit forEach Lambda Implementation : Key : 65 Value : noni Key : 33 Value : yogi Key : 98 Value : yashi Key : 37 Value : isha Key : 54 Value : yashu Key : 73 Value : dv Key : 46 Value : pulkit |
Example 2. Map with forEach (conditional)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java . util . HashMap ; import java . util . Map ; public class ForEach_Java8_Map_Conditional_Example { public static void main ( String [ ] args ) { Map < Integer , String > users = new HashMap < Integer , String > ( ) ; users . put ( 65 , "noni" ) ; users . put ( 33 , "yogi" ) ; users . put ( 73 , "dv" ) ; users . put ( 46 , "pulkit" ) ; users . put ( 37 , "isha" ) ; users . put ( 98 , "yashi" ) ; users . put ( 54 , "yashu" ) ; //Map Conditional check System . out . println ( "Print users whose name starts with 'y' :\n" ) ; users . forEach ( ( id , name ) - > { if ( name . startsWith ( "y" ) ) { System . out . println ( "Key: " + id + " Value: " + name ) ; } } ) ; } } |
Output:
| Print users whose name starts with 'y' : Key : 33 Value : yogi Key : 98 Value : yashi Key : 54 Value : yashu |
Example 3. List with forEach
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import java . util . ArrayList ; import java . util . List ; import java . util . function . Consumer ; public class ForEach_Java8_List_Example { public static void main ( String [ ] args ) { List < String > users = new ArrayList < String > ( ) ; users . add ( "noni" ) ; users . add ( "yogi" ) ; users . add ( "dv" ) ; users . add ( "pulkit" ) ; users . add ( "isha" ) ; users . add ( "yashi" ) ; users . add ( "yashu" ) ; //Anonymous Class Implementation System . out . println ( "forEach Anonymous Class Implementation:\n" ) ; users . forEach ( new Consumer < String > ( ) { @Override public void accept ( String v ) { System . out . println ( "List user names: " + v ) ; } } ) ; //Lambda Implementation System . out . println ( "\nforEach Lambda Implementation:\n" ) ; users . forEach ( ( v ) - > System . out . println ( "List user names: " + v ) ) ; //Java 8 Method Reference System . out . println ( "\nJava 8 Method Reference Implementation:\n" ) ; System . out . println ( "List user names: \n" ) ; users . forEach ( System . out : : println ) ; //Java 8 Stream Implementation System . out . println ( "\nJava 8 Stream Implementation Implementation:\n" ) ; users . stream ( ) . forEach ( ( v ) - > System . out . println ( "List user names: " + v ) ) ; } } |
Output:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | forEach Anonymous Class Implementation : List user names : noni List user names : yogi List user names : dv List user names : pulkit List user names : isha List user names : yashi List user names : yashu forEach Lambda Implementation : List user names : noni List user names : yogi List user names : dv List user names : pulkit List user names : isha List user names : yashi List user names : yashu Java 8 Method Reference Implementation : List user names : noni yogi dv pulkit isha yashi yashu Java 8 Stream Implementation Implementation : List user names : noni List user names : yogi List user names : dv List user names : pulkit List user names : isha List user names : yashi List user names : yashu |
Example 4. List with forEach (conditional)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java . util . ArrayList ; import java . util . List ; public class ForEach_Java8_List_Conditional_Example4 { public static void main ( String [ ] args ) { List < String > users = new ArrayList < String > ( ) ; users . add ( "noni" ) ; users . add ( "yogi" ) ; users . add ( "dv" ) ; users . add ( "pulkit" ) ; users . add ( "isha" ) ; users . add ( "yashi" ) ; users . add ( "yashu" ) ; //List Conditional check System . out . println ( "Check the list if contains the user with name 'pulkit':\n" ) ; users . forEach ( ( name ) - > { if ( name . equals ( "pulkit" ) ) { System . out . println ( "Expected name 'pulkit' is present in the list" ) ; } } ) ; } } |
Output:
| Check the list if contains the user with name 'Tommy' : Expected name 'pulkit' is present in the list |
Iterate Map & List using Java 8 forEach...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Double the even / odd numbers of a specified ArrayList using Streams
- Double the numbers of specified ArrayList using Streams
Other Useful References:
- java.util.stream by Oracle
- Java 8 Stream by Baeldung
rodriguezhantimpok.blogspot.com
Source: https://techndeck.com/java-8-foreach-examples-map-list/
0 Response to "Java 8 Api Continue in Foreach"
Post a Comment