header

Java Topics

There are three main features in Java 8 that I love, one of them is the lambda expressions is like syntactic sugar for an anonymous class, it will have enormous implications for simplifying development. The another one is the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements. The Stream API is integrated into the Collections API, which enables bulk operations on collections, such as sequential or parallel map-reduce transformations. And lastly but not least the java.time API that now are compatible with lambda, thread safe and simplified usage.

forEach() in a Map

Iterate over a collection in Java 8 is a really nice one, which lets you pass a method reference or a lambda to receive (key, value) pairs one by one.

import java.util.Map;
import java.util.HashMap;

class ForEachMap {

  void iterate(Map<String, Integer> items){
    items.forEach((k,v)->{
      System.out.println("Item : " + k + " Count : " + v);
    });
  }

  public static void main(String[] args){
    Map<String, Integer> items = new HashMap<>();
    items.put("A", 10);
    items.put("B", 20);
    items.put("C", 30);

    new ForEachMap().iterate(items);
  }

}

output

Item : A Count : 10
Item : B Count : 20
Item : C Count : 30

forEach() in a List

Also, you can loop a List with forEach

import java.util.List;
import java.util.ArrayList;

class ForEachList {

  void iterate(List<String> items){
    items.forEach(System.out::println);
  }

  public static void main(String[] args){
    List<String> items = new ArrayList<String>();
    items.add("A");
    items.add("B");
    items.add("C");

    new ForEachList().iterate(items);
  }

}

output

A
B
C

NOTE: You can get item from collection like this: items.forEach(item -> System.out.println(item));

You can iterate a Set in the same way

Return to the main article

comments powered by Disqus