栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java去重

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java去重

在Java中对Javabean进行去重

		List personList = new ArrayList();
        personList.add(new Person("Tom", 8900, 10,"male", "New York"));
        personList.add(new Person("Jack", 7000, 20,"male", "Washington"));
        personList.add(new Person("Lily", 7800, 30,"female", "Washington"));
        personList.add(new Person("Anni", 8500, 40,"female", "New York"));
        personList.add(new Person("Owen", 9500, 25,"male", "New York"));
        personList.add(new Person("Alisa", 7900, 24,"female", "New York"));
        personList.add(new Person("Alisa", 8500, 24,"female", "New York"));

//		  方法一:根据name去重
        ArrayIter collect6 = personList.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(o -> o.getName()))
                ), ArrayIter::new)
        );
        
        System.out.println(collect6);
        
//        方法二:
        TreeSet people = new TreeSet<>(Comparator.comparing(Person::getName));
        people.addAll(personList);
        ArrayList collect7 = new ArrayList<>(people);

        System.out.println(collect7);
        
//        方法三:使用hutool
        TreeSet set = CollectionUtil.toTreeSet(personList, Comparator.comparing(Person::getName));
        ArrayList collect8 = CollectionUtil.newArrayList(set);

        System.out.println(collect8);


在java中对list进行去重

 		ArrayList list = CollectionUtil.newArrayList(50, 20, 60, 79, 10, 50, 60, 100);
        List list1 = list.stream().distinct().collect(Collectors.toList());
        
        System.out.println(list1);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571807.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号