对于 String 列表的去重因为 String 类已经覆写了 equals() 和 hashCode() 方法,所以可以去重成功。
1.import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class hhhh { public static void main(String[] args) { // TODO Auto-generated method stub List<String> list = Arrays.asList("a", "b", "c", "d","d","a"); System.out.println("原来的List:"); for (String s : list) { System.out.print(s); } List<String> distList = list.stream().distinct().collect(Collectors.toList()); System.out.println(); System.out.println("去重后:"); for (String s : distList) { System.out.print(s); } System.out.println(); List<String> Upper = list.stream().map(String::toUpperCase).collect(Collectors.toList()); System.out.println("大写后:"); for(String s:Upper){ System.out.print(s); } System.out.println(); //{A, B, C, D} } }