top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to use forEach() method as loop in Java 8?

+2 votes
434 views

How to use forEach() method as loop in Java 8, please explain with example?

posted Jan 12, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

forEach and Map

1.1 Normal way to loop a Map.

Map<String, Integer> items = new HashMap<>();
    items.put("A", 10);
    items.put("B", 20);
    items.put("C", 30);
    items.put("D", 40);
    items.put("E", 50);
    items.put("F", 60);

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

1.2 In Java 8, you can loop a Map with forEach + lambda expression.

Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);

items.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));

items.forEach((k,v)->{
    System.out.println("Item : " + k + " Count : " + v);
    if("E".equals(k)){
        System.out.println("Hello E");
    }
});

2. forEach and List

2.1 Normal for-loop to loop a List.

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

    for(String item : items){
        System.out.println(item);
    }

2.2 In Java 8, you can loop a List with forEach + lambda expression or method reference.

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

    //lambda
    //Output : A,B,C,D,E
    items.forEach(item->System.out.println(item));

    //Output : C
    items.forEach(item->{
        if("C".equals(item)){
            System.out.println(item);
        }
    });

    //method reference
    //Output : A,B,C,D,E
    items.forEach(System.out::println);

    //Steam and filter
    //Output : B
    items.stream()
        .filter(s->s.contains("B"))
        .forEach(System.out::println);
answer Jan 19, 2016 by Karthick.c
Similar Questions
0 votes

I am working on an API integration. Now I have a problem. First I am trying to show all specific city hotels, which city is having in my database. First FOREACH worked as well its search hotel by my database city, but on Second FOREACH I am trying to show First FOREACH city hotels show only match hotel which hotels matches from database and its not worked.

My Task is: 1st search by city which city have on my database and 2nd time search in 1st time hotel and showed hotel only match my database hotel.

1st foreach:

if($RowCount>0){
            foreach($Results as $Result){
                foreach($api_array as &$value){
                    if($Result['county'] == $value['address']['city']){ 
                        $final_array[] = $value; 
                    } 
                } 
            }
        }

2nd foreach:

foreach($final_array as &$display){
            var_dump($display);
            if($Result['hotel'] == $display['property_name']){ 
                $final_array2[] = $display; 
            }  
        }

full code:

foreach($array as $api_array){
        $final_array = array(); 
        $Results = $wpdb->get_results( "select * FROM hotels where county = '$countyname'",ARRAY_A );
        $RowCount  =  $wpdb->num_rows;

        if($RowCount>0){
            foreach($Results as $Result){
                foreach($api_array as &$value){
                    if($Result['county'] == $value['address']['city']){ 
                        $final_array[] = $value; 
                    } 
                } 
            }
        }
        foreach($final_array as &$display){
            if($Result['hotel'] == $display['property_name']){ 
                $final_array2[] = $display; 
            }  
        }
    }
...