How to remove duplicate words from List in Java

1 Answer

0 votes
import java.util.*;
import java.util.stream.Collectors; 
  
public class MyClass {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>(Arrays.asList("java", "c", "java", "c++", 
                                                          "c#", "c", "java", "php")); 
  
        System.out.println(list); 
          
        list = list.stream().distinct().collect(Collectors.toList()); 
  
        System.out.println(list); 
    }
}
      
      
      
      
/*
run:
      
[java, c, java, c++, c#, c, java, php]
[java, c, c++, c#, php]
  
*/

 



answered Apr 21, 2020 by avibootz
...