Java 查询List中的重复值

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mc_linfen/article/details/82115496

查询List中重复值

public class Test {

    public static void main(String[] args) {

        List<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("2");
        strings.add("3");
        strings.add("3");
        strings.add("3");

        Set<String> set = new HashSet<>();
        Set<String> exist = new HashSet<>();

        for (String s : strings) {
            if (set.contains(s)) {
                exist.add(s);
            } else {
                set.add(s);
            }
        }
        System.out.println("重复的值:" + String.join(", ", exist));
    }
    
}

猜你喜欢

转载自blog.csdn.net/mc_linfen/article/details/82115496