1 Map 集合的操作
1.1 map集合转换为List或者Set
Map<String,String> data = new HashMap<>();
data.put("one","1");
data.put("two","2");
List<String> dataList = data.entrySet()
.stream()
.map(Map.Entry::getValue)
.collect(Collectors.toList());
Map<String,String> data = new HashMap<>();
data.put("one","1");
data.put("two","2");
List<String> dataList = data.values()
.stream()
.collect(Collectors.toList());
1.2 map 集合value总和
Map<String,String> data = new HashMap<>();
data.put("one","1");
data.put("two","2");
long sum = data.entrySet().stream()
.filter(entry -> !entry.getKey().equals("one"))
.mapToLong(entry -> Long.parseLong(entry.getValue())).sum();
1.3 合并两个map,key值相同的value,进行求和
Map<String,Integer> data1 = new HashMap<>();
data1.put("one",1);
data1.put("two",2);
Map<String,Integer> data2 = new HashMap<>();
data2.put("one",1);
data2.put("three",3);
data1.forEach((key, value) -> data2.merge(key, value, Integer::sum));
System.out.println(new ArrayList<>(data2.values()));
输出 [2, 2, 3]
2 对List集合的操作
2.1 根据时间对list集合的对象进行升序排序
List<TestDTO> list = new ArrayList<>();
list.add(new TestDTO(1,"12"));
list.add(new TestDTO(2,"222"));
list.add(new TestDTO(3,"1111"));
List<TestDTO> resultList = list.stream()
.sorted(Comparator.comparing(TestDTO::getTime)).collect(Collectors.toList());
@Data
@AllArgsConstructor
class TestDTO{
private int id;
private String time;
}
输出 [TestDTO(id=3, time=1111), TestDTO(id=1, time=12), TestDTO(id=2, time=222)]
2.2 stream流根据时间筛选所需数据
List<CameraAlarmDto> collect = cameraAlarms.stream().
filter(e -> (e.getTimeLast().after(startDate) &&
e.getTimeLast().before(endDate))).collect(Collectors.toList());
2.3 对list集合根据某一个元素进行过滤,并选取过滤后的集合对象中的某些属性组合成新的集合
groups.forEach(group -> {
List<DceEquipGroupInfoVo> dceEquipGroupInfoVos = dceEquipInfoDtos.stream().
filter((DceEquipGroupInfoDto d) -> group.getNodeName().equals(d.getNodeName())).
map(item -> {
DceEquipGroupInfoVo equipGroupInfoVo = new DceEquipGroupInfoVo();
equipGroupInfoVo.setEquipKey(item.getEquipKey()).setEquipName(item.getEquipName());
return equipGroupInfoVo;
}).distinct().collect(Collectors.toList());
2.4 对List集合使用分页查询
thirdCloudHostDtos = thirdCloudHostDtos.stream().skip((pageNum - 1) * pageSize)
.limit(pageSize).collect(Collectors.toList());
2.5 list集合中对象中属性为字符串数字比较倒叙排序
List<MetricVO> listTop5 = list.stream().sorted(Comparator.comparing(MetricVO::getValue,
Comparator.comparingInt(Integer::parseInt)).reversed()).limit(5).collect(Collectors.toList());
2.6 使用stream流进行模糊查询
keyWordsList = dtoList.stream().filter(e -> Boolean.FALSE ? e.getServerIp().equals(keyWords)
|| e.getServerPort().equals(keyWords) || e.getClientIp().equals(keyWords)
|| e.getClientPort().equals(keyWords) || e.getAppProgram().equals(keyWords)
|| e.getProtocol().equals(keyWords) :
(e.getServerIp().contains(keyWords)) || e.getServerPort().contains(keyWords) || e.getClientIp().contains(keyWords)
|| e.getClientPort().contains(keyWords) || e.getAppProgram().contains(keyWords)
|| e.getProtocol().contains(keyWords)).collect(Collectors.toList());
2.7 取多个list集合的交集
dealedList = keyWordsList.stream().filter(appList::contains).filter(protocolList::contains).collect(Collectors.toList());
2.8 根据对象某一字段组成类型集合
List<String> appTypes = dtoList.stream().filter(item ->item.getAppProgram() != null)
.map(HistoricalFlowDto::getAppProgram).distinct().collect(Collectors.toList());
2.9 求list属性中某一属性的和
Integer totalOrders = usedStatisticsVoList.stream().mapToInt(ServiceUsedStatisticsVo :: getWorkOrderNum).sum(); //list中存储的是对象
Long totalOrders = mapList.stream().mapToLong((s) -> Long.valueOf(String.valueOf(s.get("num")))).sum(); //list中存储的是map集合
2.10 根据list中对象的某一属性组合成新的集合
List<String> instanceIds = instances.stream().map(ThirdCloudInstance::getInstanceId).collect(Collectors.toList());
2.11 按照list对象中的某一属性进行分组
Map<String, List<MonitorBusyDegreeResult>> collect = list.stream()
.collect(Collectors.groupingBy(MonitorBusyDegreeResult::getDataDesc));
2.12 list中存储对象,根据对象某一属性排序
List< MetricVO> metricVosBusy = new ArrayList<>();
businessSystems.stream().sorted(Comparator.comparing(MonitorBusyDegreeResult::getBusyDegree, Comparator.reverseOrder())).collect(Collectors.toList())
.forEach(e -> {
metricVosBusy.add(new MetricVO().setKey(e.getDataDesc()).setValue(String.valueOf(e.getBusyDegree())));
});
2.13 list中存储map,按map中的某一key对应的value进行排序,取排行
//正序//
List<Map<String, Object>> listTop9 = list.stream()
.sorted(Comparator.comparing(e -> org.apache.commons.collections.MapUtils.getLong(e, "count")))
.limit(9)
.collect(Collectors.toList());
//逆序
List<Map<String, Object>> listTop9 = list.stream()
.sorted((c1, c2) -> org.apache.commons.collections.MapUtils.getDouble(c2, "count")
.compareTo(org.apache.commons.collections.MapUtils.getDouble(c1, "count")))
.limit(9)
.collect(Collectors.toList());