List集合日常总结

1.List集合一次性添加多个元素

public class ListAddMore {
    public static void main(String[] args) {
        List<String> listAddMore = new ArrayList<>();
        //一次性对集合添加多个元素
        Collections.addAll(listAddMore, "time", "money", "life");
        System.out.println(listAddMore);
        //打印结果:[time, money, life]
    }
}

2.List集合去重

方式一:使用java8新特性stream进行List去重

public class ListDistinct {
    public static void main(String[] args) {
        List<String> listAddMore = new ArrayList<>();
        //一次性对集合添加多个元素
        Collections.addAll(listAddMore, "time", "money", "life", "money");
        System.out.println("去重前:" + listAddMore);
        // 打印结果:去重前:[time, money, life, money]
        List<String> distinctList = listAddMore.stream().distinct().collect(Collectors.toList());
        System.out.println("去重后:" + distinctList);
        // 打印结果:去重后:[time, money, life]
    }
}

方法二:set集合判断去重,不打乱顺序

public class ListDistinct {
    public static void main(String[] args) {
        List<String> listAddMore = new ArrayList<>();
        //一次性对集合添加多个元素
        Collections.addAll(listAddMore, "time", "money", "life", "money");
        System.out.println("去重前:" + listAddMore);
        // 打印结果:去重前:[time, money, life, money]
        Set<String> set = new HashSet<>();
        List<String> distinctList = new LinkedList<>();
        for (String e : listAddMore) {
            if (set.add(e)) {
                distinctList.add(e);
            }
        }
        System.out.println("去重后:" + distinctList);
        // 打印结果:去重后:[time, money, life]
    }
}

方法三:set和list转换去重(不能保证顺序)

public class ListDistinct {
    public static void main(String[] args) {
        List<String> listAddMore = new ArrayList<>();
        //一次性对集合添加多个元素
        Collections.addAll(listAddMore, "time", "money", "life", "money");
        System.out.println("去重前:" + listAddMore);
        // 打印结果:去重前:[time, money, life, money]
        Set<String> set = new HashSet<>(listAddMore);
        List<String> distinctList = new LinkedList<>(set);
        System.out.println("去重后:" + distinctList);
        // 去重后:[money, time, life]
    }
}

3.List对象集合里面针对某一个属性进行去重

public class 针对集合中某个属性进行去重 {
    public static void main(String[] args) {

        PersonInfo personInfo1 = new PersonInfo("张三", 20);
        PersonInfo personInfo2 = new PersonInfo("张三", 18);
        PersonInfo personInfo3 = new PersonInfo("李四", 39);
        PersonInfo personInfo4 = new PersonInfo("李四", 39);
        PersonInfo personInfo5 = new PersonInfo("王五", 39);
        List<PersonInfo> list = new ArrayList<>();
        Collections.addAll(list, personInfo1, personInfo2, personInfo3, personInfo4, personInfo5);
        TreeSet<PersonInfo> collect = list.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PersonInfo::getName))));
        // 针对name属性 进行去重
        System.out.println(collect);//[PersonInfo(name=张三, age=20), PersonInfo(name=李四, age=39), PersonInfo(name=王五, age=39)]
        // 正对age属性 进行去重
        TreeSet<PersonInfo> collect1 = list.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PersonInfo::getAge))));
        System.out.println(collect1);//[PersonInfo(name=张三, age=18), PersonInfo(name=张三, age=20), PersonInfo(name=李四, age=39)]
    }
}

4.List集合取交集

retainAll() 方法用于保留 arraylist 中在指定集合中也存在的那些元素,也就是删除指定集合中不存在的那些元素。

public class Jiaoji {

    public static void main(String[] args) {
        List<String> listA = new ArrayList<>();
        List<String> listB = new ArrayList<>();

        listA.add("凯");
        listA.add("鲁班");
        listB.add("鲁班");
        listB.add("甄姬");

        listA.retainAll(listB);
        System.out.println(listA); //[鲁班]

    }

}

5.List集合取并集

removeAll() 方法用于删除存在于指定集合中的动态数组元素。

addAll() 方法将给定集合中的所有元素添加到 arraylist 中。

public class 交集 {

    public static void main(String[] args) {
        List<String> listA = new LinkedList<>();
        List<String> listB = new ArrayList<>();

        listA.add("凯");
        listA.add("鲁班");
        listB.add("鲁班");
        listB.add("甄姬");

        listA.removeAll(listB);
        System.out.println(listA);//[凯]
        listA.addAll(listB);
        System.out.println(listA); //[凯, 鲁班, 甄姬]

    }

}

6.List集合取差集

public class chaji {

    public static void main(String[] args) {
        List<String> listA = new LinkedList<>();
        List<String> listB = new ArrayList<>();

        listA.add("凯");
        listA.add("鲁班");
        listB.add("鲁班");
        listB.add("甄姬");

        listA.removeAll(listB);
        System.out.println(listA);//[凯]


    }

}
原文地址:https://www.cnblogs.com/lu97/p/15707658.html