Pages

distinct in Java

 distinct() in java

  • distinct() is used to get distinct (different) values.
  • A list may contain duplicate values, and you want to list the different values, then distinct() will be used.
distinct Java program:

import java.util.*;
import java.util.stream.Collectors;
public class Test {
public static void main(String args[]) {
List<String> keywords = Arrays.asList("email", "email", "EMAIL desk", "ABCDE");
List<String> distinct = keywords.stream().distinct().collect(Collectors.toList());
System.out.println(distinct);

List<Character> chars = Arrays.asList('7', '1', '1', '2', '3', '4', '6', '4');
List<Character> distinctChars = chars.stream().distinct().collect(Collectors.toList());
System.out.println(distinctChars);
}
}
Output:
remove duplicates in list
remove duplicates in list


Observe, by using distinct we removed duplicates from list.
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.