Java8 - sum求和,将 List 集合转为 Map,key去重(groupingBy),sorted排序
package com.example.core.mydemo.java8; public class GoodsPriceDTO { private Integer id; private String goodName; private Integer amount; //重写toString方法,System可以打印输出对象 @Override public String toString() { return "GoodsPriceDTO{" + "id=" + id + ", goodName='" + goodName + '\'' + ", amount=" + amount + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getGoodName() { return goodName; } public void setGoodName(String goodName) { this.goodName = goodName; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } }
package com.example.core.mydemo.java8; import java.util.*; import java.util.stream.Collectors; public class GoodsTotalTest { public static void main(String[] args) { List<GoodsPriceDTO> goodsPriceDTOS = new ArrayList<GoodsPriceDTO>(); GoodsPriceDTO dto = new GoodsPriceDTO(); dto.setId(1); dto.setGoodName("Apple1"); dto.setAmount(27); goodsPriceDTOS.add(dto); GoodsPriceDTO dto2 = new GoodsPriceDTO(); dto2.setId(2); dto2.setGoodName("Orange"); dto2.setAmount(28); goodsPriceDTOS.add(dto2); GoodsPriceDTO dto3 = new GoodsPriceDTO(); dto3.setId(3); dto3.setGoodName("Banana"); dto3.setAmount(29); goodsPriceDTOS.add(dto3); GoodsPriceDTO dto4 = new GoodsPriceDTO(); dto4.setId(4); dto4.setGoodName("Apple4"); dto4.setAmount(23); goodsPriceDTOS.add(dto4); //重复键key GoodsPriceDTO dto5 = new GoodsPriceDTO(); dto5.setId(4); dto5.setGoodName("Apple5"); dto5.setAmount(100); goodsPriceDTOS.add(dto5); int amt = Optional.ofNullable(goodsPriceDTOS) .orElseGet(ArrayList::new) .stream() .filter(x -> x != null && ("Apple".equals(x.getGoodName()))).mapToInt(GoodsPriceDTO::getAmount).sum(); System.out.println("amt=" + amt); //Java8 - 将 List 集合转为 Map //List转map Map<Integer,GoodsPriceDTO> map = Optional.ofNullable(goodsPriceDTOS) .orElseGet(ArrayList::new) .stream().collect(Collectors.toMap(GoodsPriceDTO::getId,GoodsPriceDTO->GoodsPriceDTO,(oldValue,newValue)->oldValue)); System.out.println("map=" + map); //List转map -2 Map<Integer,String> map2 = Optional.ofNullable(goodsPriceDTOS) .orElseGet(ArrayList::new) .stream().collect(Collectors.toMap(GoodsPriceDTO::getId,GoodsPriceDTO::getGoodName,(oldValue,newValue)->oldValue)); System.out.println("map2=" + map2); //如果出现相同的key,那么会抛出重复key的异常 //Duplicate key com.example.core.mydemo.java8.GoodsPriceDTO@20ad9418 //输出: map2={1=Apple1, 2=Orange, 3=Banana, 4=Apple4} //刚才上面出现重复的ID,是根据值进行覆盖,在某些情况下需要映射成列表。即:List -> Map<Integer, List>的情况。 Map<Integer,List<GoodsPriceDTO>> map3 = Optional.ofNullable(goodsPriceDTOS) .orElseGet(ArrayList::new) .stream().collect(Collectors.groupingBy(GoodsPriceDTO::getId)); System.out.println("map3=" + map3); //map3={1=[GoodsPriceDTO{id=1, goodName='Apple1', amount=27}], 2=[GoodsPriceDTO{id=2, goodName='Orange', amount=28}], 3=[GoodsPriceDTO{id=3, goodName='Banana', amount=29}], 4=[GoodsPriceDTO{id=4, goodName='Apple4', amount=23}, GoodsPriceDTO{id=4, goodName='Apple5', amount=100}]} //Comparator 升序 List<GoodsPriceDTO> newSortedList = Optional.ofNullable(goodsPriceDTOS) .orElseGet(ArrayList::new) .stream().sorted(Comparator.comparing(GoodsPriceDTO::getAmount)).collect(Collectors.toList()); //按价格排序: newSortedList.stream().forEach(x -> { System.out.println("x=" + x); }); //Comparator 降序 List<GoodsPriceDTO> newSortedList1 = Optional.ofNullable(goodsPriceDTOS) .orElseGet(ArrayList::new) .stream().sorted(new Comparator<GoodsPriceDTO>() { @Override public int compare(GoodsPriceDTO o1, GoodsPriceDTO o2) { return o2.getAmount().compareTo(o1.getAmount()); } }).collect(Collectors.toList()); //按价格排序: newSortedList1.stream().forEach(x -> { System.out.println("x1=" + x); }); //升序 List<GoodsPriceDTO> newSortedList2 = Optional.ofNullable(goodsPriceDTOS) .orElseGet(ArrayList::new) .stream().sorted((a,b) ->(a.getAmount() - b.getAmount())).collect(Collectors.toList()); //按价格排序: newSortedList2.stream().forEach(x -> { System.out.println("x2=" + x); }); //降序 List<GoodsPriceDTO> newSortedList3 = Optional.ofNullable(goodsPriceDTOS) .orElseGet(ArrayList::new) .stream().sorted((a,b) ->(b.getAmount() - a.getAmount())).collect(Collectors.toList()); //按价格排序: newSortedList3.stream().forEach(x -> { System.out.println("x3=" + x); }); } }
标签:Map,stream,groupingBy,goodsPriceDTOS,sum,GoodsPriceDTO,amount,new,goodName From: https://www.cnblogs.com/oktokeep/p/17304708.html