Java: using stream api

Example:

List numbers = Arrays.asList(1, 2, 3, 4, 5);
Java Stream API provides a way to process sequences of elements.

Solution:

List squared = numbers.stream().map(x -> x * x).collect(Collectors.toList());

Beginner's Guide to Java