jdk8 stream高级使用方法
@Data public class Order { private String account; private String orderNo; private Long quantity; private BigDecimal amount; private Status status; } public enum Status { start,process,finish; }
public class OrderStreamExample { public static void main(String[] args) { OrderStreamExample example = new OrderStreamExample(); example.baseOperation(); example.stream(); } public void baseOperation(){ IntConsumer action = v-> System.out.print(v+"\t"); //int startInclusive, int endExclusive IntStream.range(1,7).forEachOrdered(action); System.out.println(); List<Integer> intList = Arrays.asList(40, 32, 53, 22, 11, 89, 76); int max = intList.stream() .max(Comparator.comparing(Integer::intValue)).get(); int min = Arrays.asList(BigDecimal.valueOf(20),BigDecimal.valueOf(30),BigDecimal.valueOf(25)).stream() .min(Comparator.comparing(BigDecimal::intValue)).get().intValue(); System.out.println("max="+max+",\tmin="+min); int sum1 = intList.stream().collect(Collectors.summingInt(o->o)); int sum2 = intList.stream().reduce(0,Integer::sum); int sum3 = intList.stream().reduce(0,(a,b)->a+b); System.out.println("求和sum:\t"+sum1+"\t,"+sum2+"\t,"+sum3); IntSummaryStatistics statistics = intList.parallelStream().collect(Collectors.summarizingInt(o->o)); System.out.println("sum="+statistics.getSum()+",\t平均:"+statistics.getAverage()+",\t个数:"+statistics.getCount()+",\t最大数:"+statistics.getMax()); List<String> propList = Arrays.asList("profile=native", "debug=true", "logging=warn", "interval=500", "interval=1000"); Map<String,String> propMap = new ConcurrentHashMap<>(); propList.stream().forEach(o->{ String[] kv = o.split("\\=",2); propMap.put(kv[0],kv[1]); }); System.out.println("properties转换:"+new Gson().toJson(propMap)); System.out.println("======================="); } public void stream(){ List<Order> orderList = buildOrderList(); //默认升序,排序只能使用stream List<Order> sortedList = orderList.stream().sorted(Comparator.comparing(Order::getAmount)).collect(toList()); System.out.println("默认升序"); System.out.println(new Gson().toJson(sortedList)); //降序 List<Order> sortedReversedList = orderList.stream().sorted(Comparator.comparing(Order::getAmount).reversed()).collect(toList()); System.out.println("降序"); System.out.println(new Gson().toJson(sortedReversedList)); //按account分组 Map<String, List<Order>> accountGroupList = orderList.stream().collect(Collectors.groupingBy(o->o.getAccount())); //按account分组最大的金额 Map<String,BigDecimal> accountMaxAmountMap = orderList.stream().collect( Collectors.groupingBy(o->o.getAccount(), Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(Order::getAmount)) ,f->f.map(o->o.getAmount()).get()))); System.out.println("按account分组最大的金额"); System.out.println(new Gson().toJson(accountMaxAmountMap)); //按account分组金额最大的对象 Map<String,Order> accountMaxOrderMap = orderList.stream().collect( Collectors.groupingBy(o->o.getAccount(), Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(Order::getAmount)) ,Optional::get))); //f->f System.out.println("按account分组金额最大的对象"); System.out.println(new Gson().toJsonTree(accountMaxOrderMap)); //按status分组统计纪录数 Map<Status,Long> statusCountMap = orderList.stream().collect(Collectors.groupingBy(o->o.getStatus(),Collectors.counting())); System.out.println("按status分组统计纪录数"); System.out.println(new Gson().toJsonTree(statusCountMap)); //按account分组统计总金额 Map<String,BigDecimal> accountSumAmountMap = orderList.stream().collect( Collectors.groupingBy(o->o.getAccount() ,Collectors.mapping(o->o.getAmount(),reducing(BigDecimal.ZERO,BigDecimal::add))) ); System.out.println("按account分组统计总金额"); System.out.println(new Gson().toJsonTree(accountSumAmountMap)); //按account分组,把quantity组装字符串 Map<String,String> accountQuantityMap = orderList.stream().collect( Collectors.groupingBy(o->o.getAccount() , Collectors.mapping(o->String.valueOf(o.getQuantity()),joining(",","[","]"))) ); System.out.println("按account分组,把quantity组装字符串"); System.out.println(new Gson().toJson(accountQuantityMap)); //按account分组,封装成新对象 Map<String,OrderVO> accountOrderVOMap = orderList.stream().collect( Collectors.groupingBy(o->o.getAccount() ,Collectors.collectingAndThen(toList(),list->{ OrderVO vo = new OrderVO(); vo.setAccount(list.stream().findAny().get().getAccount()); vo.setQuantity(list.stream().map(o->o.getQuantity()).reduce(0L,Long::sum)); vo.setAmount(list.stream().map(o->o.getAmount()).reduce(BigDecimal.ZERO,BigDecimal::add)); return vo; }) ) ); System.out.println("按account分组,封装成新对象"); System.out.println(new Gson().toJson(accountOrderVOMap)); } private List<Order> buildOrderList(){ List<Order> list = new ArrayList<>(); list.add(buildOrder("tom","D1001",100L,new BigDecimal("100"),Status.start)); list.add(buildOrder("jerry","J1001",140L,new BigDecimal("200"),Status.process)); list.add(buildOrder("tom","D1002",200L,new BigDecimal("220"),Status.finish)); list.add(buildOrder("jerry","J1002",500L,new BigDecimal("500"),Status.start)); list.add(buildOrder("tom","D1003",300L,new BigDecimal("300"),Status.finish)); list.add(buildOrder("tom","D1004",400L,new BigDecimal("400"),Status.process)); list.add(buildOrder("jerry","J1003",300L,new BigDecimal("300"),Status.start)); list.add(buildOrder("tom","D1005",550L,new BigDecimal("550"),Status.process)); list.add(buildOrder("tom","D1006",50L,new BigDecimal("50"),Status.process)); list.add(buildOrder("jerry","J1004",150L,new BigDecimal("150"),Status.finish)); return list; } private Order buildOrder(String account, String orderNo, Long quantity, BigDecimal amount, Status status){ Order order = new Order(); order.setAccount(account); order.setOrderNo(orderNo); order.setQuantity(quantity); order.setAmount(amount); order.setStatus(status); return order; } } @Data class OrderVO{ private String account; private Long quantity; private BigDecimal amount; }