Java8_forEachExample



   

 



This   example   forEach()   easily   traverse  the  collection .  You  can compare  this
 example using java 7 .



package Java8;

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

public class HelloJava8 {


public  static  void  getDataList() { 

   List items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
items.add("M");
items.add("N");
items.add("Z");

        items.forEach((i)->System.out.println("List::"+i ));                   

}   
         
    public  static  void  getDataHashMap (){
   
     Map  < String , Integer >  ma= new  HashMap<>() ;
                ma.put("A",  9);
                ma.put("B", 7);
                ma.put("C", 11);
                ma.put("G", 5);

           ma.forEach((k , v)->System.out.println("Key::"+k +"--" + "Value::"  +v ));
    }
       
 
  public static void main(String[] args) {
// TODO Auto-generated method stub

getDataList();
getDataHashMap();
}

}

  When   you run this code   this will  be the out put.


List::A
List::B
List::C
List::D
List::E
Key::A--Value::9
Key::B--Value::7
Key::C--Value::11

Key::G--Value::5
  

Java8_forEachExample Java8_forEachExample Reviewed by Mukesh Jha on 8:23 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.