Convert IntStream to Map

Here is my code, it will work for you.

Function Reference version

public class AppLauncher {

public static void main(String a[]){
    Map<Integer,Integer> map = IntStream.range(1,10).boxed().collect(Collectors.toMap(Function.identity(),AppLauncher::computeSmth));
    System.out.println(map);
}
  public static Integer computeSmth(Integer i){
    return i*i;
  }
}

Lambda expression version

public class AppLauncher {

    public static void main(String a[]){
        Map<Integer,Integer> map = IntStream.range(1,10).boxed().collect(Collectors.toMap(Function.identity(),i->i*i));
        System.out.println(map);
    }
}

Leave a Comment

tech