集合中取某一个字段求和
BigDecimal求和
BigDecimal totalPrice = list.stream().map(Apple::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
interge求和
Integer sum = integers.stream().map(Apple::getPrice).reduce(0, Integer::sum);
stream 分组条件查询
public static void main(String[] args) { Stu s1 = new Stu("机构1", 2,"商品1"); Stu s11 = new Stu("机构1", 2,"商品2"); Stu s2 = new Stu("机构1", 44,"商品44"); Stu s3 = new Stu("机构2", 2,"商品1"); Stu s4 = new Stu("机构2", 3,"商品1"); Stu s5 = new Stu("机构2", 3,"商品1"); List<Stu> list = new ArrayList(); list.add(s1);list.add(s2);list.add(s3);list.add(s4);list.add(s11);list.add(s5); //当对象中某个属性相同时 把对象的另一个值存到List集合里面 // Map<Integer, List<Stu>> map = list.stream().collect(Collectors.groupingBy(Stu::getAge)); // list.stream().collect(Collectors.groupingBy(Stu::getAgency, TreeMap::new, Collectors.mapping(Stu::getSupplier, Collectors.toList()))); // //分组根据两个条件一致分组 把分组的后的数据放到map集合里 Map<String, Map<Integer, List<Stu>>> collect = list.stream() .collect(Collectors.groupingBy(Stu::getAgency, Collectors.groupingBy(Stu::getSupplier))); collect.forEach((key,val)->{ System.out.println("机构id******:"+key+" 供应商:"+val); }); } } @Data class Stu{ String agency; int supplier; String goods; public Stu(String agency, int supplier,String goods) { super(); this.agency = agency; this.supplier = supplier; this.goods = goods; } }
List<integer> 转 string
List<String> areaIdList = openAreaItemList.stream().map(x -> x.getGoodsOpenAreaId().toString()).collect(Collectors.toList());