java8list fliter按条件 分组 转成map

mac2024-03-09  30

java8 list fliter 按条件 分组 转成map

list 进行 fliter 按条件 分组 转成map

private static void getStock() { String json = "{\"code\":\"200\",\"flag\":\"success\",\"items\":{\"item\":[{\"batchCode\":\"5112005815\",\"expireDate\":1711900800000,\"inventoryType\":\"ZP\",\"itemCode\":\"80405020002\",\"itemId\":\"80405020002\",\"locationInfos\":{\"locationInfo\":{\"locationName\":\"LS015\",\"lockQuantity\":\"0\",\"quantity\":\"102\"}},\"lockQuantity\":\"0\",\"productDate\":1711900800000,\"quantity\":\"892\",\"warehouseCode\":\"聪衡长沙仓\"},{\"batchCode\":\"5112006260\",\"expireDate\":1722268800000,\"inventoryType\":\"ZP\",\"itemCode\":\"80405020002\",\"itemId\":\"80405020002\",\"locationInfos\":{\"locationInfo\":{\"locationName\":\"KFH2\",\"lockQuantity\":\"0\",\"quantity\":\"1000\"}},\"lockQuantity\":\"0\",\"productDate\":1722268800000,\"quantity\":\"5000\",\"warehouseCode\":\"聪衡长沙仓\"},{\"inventoryType\":\"ZP\",\"itemCode\":\"80405020002\",\"itemId\":\"80405020002\",\"locationInfos\":{\"locationInfo\":{\"locationName\":\"CKHCCW\",\"lockQuantity\":\"0\",\"quantity\":\"0\"}},\"lockQuantity\":\"0\",\"quantity\":\"0\",\"warehouseCode\":\"聪衡长沙仓\"},{\"batchCode\":\"5112005815\",\"expireDate\":1711814400000,\"inventoryType\":\"ZP\",\"itemCode\":\"80405020002\",\"itemId\":\"80405020002\",\"locationInfos\":{\"locationInfo\":{\"locationName\":\"THA443\",\"lockQuantity\":\"0\",\"quantity\":\"0\"}},\"lockQuantity\":\"0\",\"productDate\":1711814400000,\"quantity\":\"0\",\"warehouseCode\":\"聪衡长沙仓\"},{\"batchCode\":\"5112005970\",\"expireDate\":1699804800000,\"inventoryType\":\"ZP\",\"itemCode\":\"80401020015\",\"itemId\":\"80401020015\",\"locationInfos\":{\"locationInfo\":{\"locationName\":\"LS026\",\"lockQuantity\":\"0\",\"quantity\":\"197\"}},\"lockQuantity\":\"0\",\"productDate\":1699804800000,\"quantity\":\"197\",\"warehouseCode\":\"聪衡长沙仓\"},{\"batchCode\":\"5112005970\",\"expireDate\":1701273600000,\"inventoryType\":\"ZP\",\"itemCode\":\"80401020015\",\"itemId\":\"80401020015\",\"locationInfos\":{\"locationInfo\":{\"locationName\":\"THQ133\",\"lockQuantity\":\"0\",\"quantity\":\"11\"}},\"lockQuantity\":\"0\",\"productDate\":1701273600000,\"quantity\":\"11\",\"warehouseCode\":\"聪衡长沙仓\"},{\"inventoryType\":\"ZP\",\"itemCode\":\"80401020015\",\"itemId\":\"80401020015\",\"locationInfos\":{\"locationInfo\":{\"locationName\":\"CKHCCW\",\"lockQuantity\":\"0\",\"quantity\":\"0\"}},\"lockQuantity\":\"0\",\"quantity\":\"0\",\"warehouseCode\":\"聪衡长沙仓\"}]},\"message\":\"接收成功\"}"; QueryStockResult result2 = JSON.parseObject(json, QueryStockResult.class); List<Item> item = result2.getItems().getItem(); List<String> skuList = new ArrayList<>(); List<Item> item3 = new ArrayList<>(); item3 = item.stream() .filter(item1 -> !item1.getLocationInfos().getLocationInfo().getLocationName().equals("CKHCCW") && !item1.getLocationInfos().getLocationInfo().getLocationName().equals("FHYCCW")) .collect(Collectors.toList()); /* //分组 Map<String, List<User>> groupBySex = userList.stream().collect(Collectors.groupingBy(User::getSex)); //遍历分组 for (Map.Entry<String, List<User>> entryUser : groupBySex.entrySet()) { String key = entryUser.getKey(); List<User> entryUserList = entryUser.getValue();*/ // 过滤 2个字符串的之后 把 库存相加赋值 item.stream() .filter(item1 -> !item1.getLocationInfos().getLocationInfo().getLocationName().equals("CKHCCW") && !item1.getLocationInfos().getLocationInfo().getLocationName().equals("FHYCCW")). forEach(item1 -> { item1.setStcokNum(item1.getStcokNum() + Integer.valueOf(item1.getLocationInfos().getLocationInfo().getQuantity())); }); //item3 = item.stream().collect(Collectors.toList()); // 第二步 判断非空之后 分组库存相加转成map Map<String, Item> collect = item.stream() .filter(Objects::nonNull) .collect(Collectors.toMap(Item::getItemCode, Function.identity(), surgeryCountMerge)); System.out.println("jsonString" + JSON.toJSONString(collect)); Map<String, List<Item>> groupByCode = item.stream() .filter(item1 -> !item1.getLocationInfos().getLocationInfo().getLocationName().equals("CKHCCW") && !item1.getLocationInfos().getLocationInfo().getLocationName().equals("FHYCCW")) .collect(Collectors.groupingBy(Item::getItemCode)); System.out.println("jsonstring2" + JSON.toJSONString(groupByCode)); } // 对应的 库存数相加 public static final BinaryOperator<Item> surgeryCountMerge = (v1, v2) -> { v1.setStcokNum(v1.getStcokNum()+v2.getStcokNum()); return v1; };
最新回复(0)