去掉List中重复的元素

方式1:将list转set

   public static void main(String[] args) {
        List words = Arrays.asList("A", "B", "C", "D", "D", "A");
        Set sets = new HashSet<>();
        for (String word : words) {
            sets.add(word);
        }
        for (String s : sets) {
            System.out.println(s);
	}

方式2:使用stream的distinct方式去重

 public static void main(String[] args) {
        List words = Arrays.asList("A", "B", "C", "D", "D", "A");
        words.stream().distinct().collect(Collectors.toList()).forEach(System.out::println);
    }

你可能感兴趣的:(Java基础,面试题)