Java Basic List to remove duplicate objects

List initialization

List<String> name = Arrays.asList("xxx","yyy","zzz");
//or
List<String> name =new ArrayList<>(Arrays.asList("xxx","yyy","zzz"));

However, in this case, the size of the list will be fixed, and it cannot be added any more, so be careful.

 

List remove duplicates

List<String> list = Arrays.asList("abc", "hello", "hello", "world");

List<String> distinctList = list.stream().distinct().collect(Collectors.toList());

System.out.println(String.join(",", distinctList));

 output abc,hello,world

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324856002&siteId=291194637