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);//[凯]


    }

}
复制代码

 

posted @   10114  阅读(116)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2020-12-19 NoClassDefFoundError
点击右上角即可分享
微信分享提示