distinct method :- The Stream.distinct() method returns a new Stream consisting of the distinct elements from the given Stream.
- It is inside stream interface,it return unique element.
- There are internally use hashcode and equal method to return distinct element.
- This is a intermediate operation.
- Traverse the list from stream then call distinct method for unique element.
- Now print data from forEach method to Iterating by passing method reference.
import java.util.Arrays;
import java.util.List;
public class DistinctMethod {
public static void main(String[] args) {
List l = Arrays.asList(12, 12, 13, 45, 18, 90);
l.stream().distinct().forEach(System.out::println);
}
}
Output :-
12
13
45
18
90