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

Java1.8对List集合操作汇总

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

Java1.8对List集合操作汇总

1、去重

List myList = listAll.stream().distinct().collect(Collectors.toList());

2、根据单个对象属性去重

list.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet(Comparator.comparing(UserCar::getCarNo))), ArrayList::new));

或者

list.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o->o.getCarNo()))), ArrayList::new));

3、根据多个对象属性去重

list.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o->o.getDetailID()+ ";" + o.receiptCode()))),ArrayList::new)); 

4、根据单个对象属性合并

list.stream().collect(Collectors.toMap(BillsNums::getId, a -> a, (o1,o2)-> {
            o1.setNums(o1.getNums() + o2.getNums());
            o1.setSums(o1.getSums() + o2.getSums());
            return o1;
        })).values().stream().collect(Collectors.toList());

5、根据多个对象属性合并

list.parallelStream().collect(Collectors.groupingBy(o -> (o.geName() + o.getAge()), Collectors.toList())).forEach((id, transfer) -> { transfer.stream().reduce((a, b) -> new Student(a.getName(), a.getAge(), a.getScore() + b.getScore())).ifPresent(studentList::add);});

或使用Map:需重写hashCode()、equals()方法

原因移步另一篇文章:Java中将对象当成map的key存在的问题与解决方案

    //新建类,作为map的key
    static class Temp {
        private String period;//会计期间
        private String dept;//部门
        private String fund;//资金用途
        private String currency;//币别

        public String getPeriod() {
            return period;
        }

        public void setPeriod(String period) {
            this.period = period;
        }

        public String getDept() {
            return dept;
        }

        public void setDept(String dept) {
            this.dept = dept;
        }

        public String getFund() {
            return fund;
        }

        public void setFund(String fund) {
            this.fund = fund;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }

        //重写hashcode方法
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((period == null) ? 0 : period.hashCode());
            result = prime * result + ((dept == null) ? 0 : dept.hashCode());
            result = prime * result + ((fund == null) ? 0 : fund.hashCode());
            result = prime * result + ((currency == null) ? 0 : currency.hashCode());
            return result;
        }

        //重写equals方法
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Temp other = (Temp) obj;
            if (period == null) {
                if (other.period != null)
                    return false;
            } else if (!period.equals(other.period))
                return false;
            if (dept == null) {
                if (other.dept != null)
                    return false;
            } else if (!dept.equals(other.dept))
                return false;
            if (fund == null) {
                if (other.fund != null)
                    return false;
            } else if (!fund.equals(other.fund))
                return false;
            if (currency == null) {
                if (other.currency != null)
                    return false;
            } else if (!currency.equals(other.currency))
                return false;
            return true;
        }
    }


    @Override
    public void method () {
         List list = new ArrayList<>();
         Map map = new HashMap<>();
         for (DynamicObject entryItem : entry) {
              Temp temp = new Temp();
              temp.setPeriod(dateStr);
              temp.setCurrency(currency);
              temp.setDept(dept);
              temp.setFund(fund);
              if (map.containsKey(temp)) {
                //累加实付金额
                entryItem.set(KEY_PAY_SFJE,map.get(temp).getBigDecimal(KEY_PAY_SFJE).add(entryItem.getBigDecimal(KEY_PAY_SFJE)));
              }
              map.put(temp, entryItem);
         }
        for (Map.Entry entry : map.entrySet()) {
            list.add(entry.getValue());
        }
    }

6、根据对象属性值获取对象

 Optional first = list.stream().filter(user -> Objects.equals(user.getId(),1)).findFirst();
       //判空
        if(first.isPresent()) { 
            System.out.println(first.get());
        }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032137.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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