JDK8集合部分相关语法
1.List转map
1.1 构造参数
ArrayList<ProductReturnDetailResult> productReturnDetailResults = new ArrayList<>();//退货结果
ProductReturnDetailResult returnDetailResult = new ProductReturnDetailResult();
returnDetailResult.setMsg("该订单状态更新成功!");
returnDetailResult.setProductNo(productSerialNos);
returnDetailResult.setStatus(true);
productReturnDetailResults.add(returnDetailResult);
1.2 使用
Map<Boolean, ProductReturnDetailResult> productResult = productReturnDetailResults.stream().collect(Collectors.toMap(ProductReturnDetailResult::isStatus,
detailResult -> detailResult, (oldDetail, newDetail) -> newDetail));
1.3 使用说明
该段代码的含义是:以isStatus(boolean)的值作为key,value使用ProductReturnDetailResult,如果key相同 那么使用新的覆盖原来的value.
2.排序
List<Integer> list1 = Arrays.asList(1, 3, 2, 5, 4);
Collections.sort(list1,(o1,o2)->o1-o2);//升序
Collections.sort(list1,(o1,o2)->o2-o1);//降序
3.for循环
ArrayList<SupplierOrder> supplierOrders = new ArrayList<>();
SupplierOrder supplierOrder = new SupplierOrder();
supplierOrder.setSupplyOrderNo("9999");
supplierOrder.setComment("test");
supplierOrders.add(supplierOrder);
supplierOrders.forEach(order -> {
System.out.println(order.getSupplyOrderNo());
System.out.println(order.getComment());
});
4.filter(保留)
ArrayList<SupplierOrder> supplierOrders = new ArrayList<>();
SupplierOrder supplierOrder = new SupplierOrder();
supplierOrder.setSupplyOrderNo("9999");
supplierOrder.setComment("test");
supplierOrders.add(supplierOrder);
SupplierOrder supplierOrder1 = new SupplierOrder();
supplierOrder1.setSupplyOrderNo("9998");
supplierOrder1.setComment("test");
supplierOrders.add(supplierOrder1);
List<SupplierOrder> newOrder = supplierOrders.stream().filter(order -> order.getSupplyOrderNo().contains("9998"))
.collect(Collectors.toList());
System.out.println(JSON.toJSONString(newOrder));
PS: filter理解成保留的意思不要理解成过滤,该结果输出为:[{"comment":"test","supplyOrderNo":"9998"}]
5.取出List中对象中的某一个字段作为一个集合
List<MerchantInvitation> merchantInvitations = new ArrayList<>();
List<Integer> merchantIds = merchantInvitations.stream().map(MerchantInvitation::getMerchantId).collect(Collectors.toList());
6.计算数值返回整形
List<InvitationStatisticsResult> list = new ArrayList<>();
int invitationCountSum = list.stream().mapToInt(InvitationStatisticsResult::getInvitationCount).sum();
或者
List<InvitationStatistics> temp = new ArrayList<>();
InvitationStatistics t = new InvitationStatistics();
t.setInvitationCount(2);
InvitationStatistics t1 = new InvitationStatistics();
t1.setInvitationCount(2);
temp.add(t);
temp.add(t1);
Integer collect = temp.stream().collect(Collectors.summingInt(InvitationStatistics::getInvitationCount));
7.计算BigDecimal
List<InvitationStatisticsResult> list = new ArrayList<>();
BigDecimal transactionAmountSum = list.stream().map(InvitationStatisticsResult::getTransactionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
8.连接字符串
List<InvitationStatistics> temp = new ArrayList<>();
InvitationStatistics t = new InvitationStatistics();
t.setMerchantName("test1");
InvitationStatistics t1 = new InvitationStatistics();
t1.setMerchantName("test2");
temp.add(t);
temp.add(t1);
String collect = temp.stream().map(InvitationStatistics::getMerchantName).collect(Collectors.joining("-"));
System.out.println(collect);
9.获取最大值
List<InvitationStatistics> temp = new ArrayList<>();
InvitationStatistics t = new InvitationStatistics();
t.setInvitationCount(2);
InvitationStatistics t1 = new InvitationStatistics();
t1.setInvitationCount(3);
temp.add(t);
temp.add(t1);
Optional<InvitationStatistics> collect = temp.stream().
collect(Collectors.maxBy(Comparator.comparingInt(InvitationStatistics::getInvitationCount)));
System.out.println(JSON.toJSONString(collect));
10.获取最小值
List<InvitationStatistics> temp = new ArrayList<>();
InvitationStatistics t = new InvitationStatistics();
t.setInvitationCount(2);
InvitationStatistics t1 = new InvitationStatistics();
t1.setInvitationCount(3);
temp.add(t);
temp.add(t1);
Optional<InvitationStatistics> collect = temp.stream().
collect(Collectors.minBy(Comparator.comparingInt(InvitationStatistics::getInvitationCount)));
System.out.println(JSON.toJSONString(collect));
11.分组
Map<Integer, List<SupplierOrder>> supplierOrderGroupBy = supplierOrders.stream().
collect(Collectors.groupingBy(SupplierOrder::getMerchantId));//根据卖家id进行分组
12.排序
List<InvitationRecordModel> result2 = invitationRecordModels.stream().filter(record -> record.getIsCreateOrder().equals(Const.MERCHANT_IS_CREATE_ORDER_FALSE))
.collect(Collectors.toList());
Collections.sort(result2,Comparator.comparing(InvitationRecordModel::getCreateDt).reversed());