Java Stream How to - Map to double each element in a list and return them as an Array








Question

We would like to know how to map to double each element in a list and return them as an Array.

Answer

import java.util.Arrays;
import java.util.List;
public class Main {
/*from ww w .  j  av  a  2  s.  com*/
  public static void main(String[] args) {
    List<Integer> _numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    Object[] doubled = _numbers.stream()
        .map(value -> value * 2)
        .toArray();
    
    System.out.println(Arrays.toString(doubled));
  }
}

The code above generates the following result.