What does it mean: a sign in java? [duplicate]
I often see ::
when defining a new instance, in streams, sometimes in lambda expressions. What does it mean? What does List :: stream
mean?
P.S. And what is it called?
1 answers
The "method reference" operator (Method Reference). In fact , it is an abbreviated entry for referring to a method from a functional interface when using a lambda.
List::stream
Is a reference to the stream()
method from the List
{[12 interface]}
List<List<Integer>> list = Arrays.asList(
Arrays.asList(1,2,3),
Arrays.asList(4,5,6)
);
list.steam()
.flatMap(List::stream)
.forEach(System.out::println);
The above code snippet means: 1. We initialize a stream that will have a list of numbers (Stream<List<Integer>>
) 2. using flatMap
we combine the list of numbers into a stream of numbers (Stream<Integer>
) 3. using forEach
and a reference to the println
method for each element from the stream is called to display
If you speak English, a good explanation with examples is given in this answer