Java 8 Stream distinct method example.
The Stream.distinct() method returns a stream consisting of the distinct elements of this stream. It uses Object.equals() method.
For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in
the encounter order is preserved.) For unordered streams, no stability guarantees are made.
package com.java2novice.streams;
import java.util.stream.Stream;
public class StreamDistinctEx {
public static void main(String a[]) {
Stream.of("bus", "car", "bycle", "bus", "car", "car", "bike")
.distinct()
.forEach(System.out::println);
}
}
|
|