Java Stream How to - Map to create new value from int array








Question

We would like to know how to map to create new value from int array.

Answer

//  w  w w . j av  a  2  s .co m
import java.util.Arrays;

public class Main {

  public static void main(String[] args) throws Exception {
    Arrays.stream(new int[] {1, 2, 3})
    .map(n -> 2 * n + 1)
    .average()
    .ifPresent(System.out::println);
  }

}

The code above generates the following result.