Java 8 distinct() Method with Example

distinct method :-  

  • The Stream.distinct() method is available in the Stream interface.
  • It returns a new stream containing only the unique elements from the original stream..
  • Internally, it uses the equals() and hashCode() methods to identify duplicate elements.
  • It is an intermediate operation in the Stream API.
  • In the example below, the list is converted into a stream, and distinct() is used to remove duplicate values.
  • Finally, the unique elements are printed using forEach() and a method reference..
				
					import java.util.Arrays;
import java.util.List;

public class DistinctMethod {
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(12, 12, 13, 45, 18, 90);
		list.stream().distinct().forEach(System.out::println);
	}
}
				
			

Output :-
12
13
45
18
90