Java Streams – Map a List


Use .map() to apply a function to each element in a stream.

Source Code

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamMap {
    public static void main(String[] args) {
        List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        List squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        System.out.println(squaresList);
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments