jdk1.8 集合操作
程序员文章站
2022-05-03 23:38:57
/** * JDK1.8的学习 */public class Test { public static void main(String[] args) { ModelService modelService = (ModelService) SpringUtil.getBean("modelServiceImpl"); // 1.对象创建: 使用lombok注解 // @Data // @....
/**
* JDK1.8的学习
*/
public class Test {
public static void main(String[] args)
{
ModelService modelService = (ModelService) SpringUtil.getBean("modelServiceImpl");
// 1.对象创建: 使用lombok注解
// @Data
// @NoArgsConstructor
// @AllArgsConstructor
//2. list或者map创建
Map<String, Integer> map = Maps.newHashMap();
List<Map.Entry<String, Integer>> dataList = Lists.newArrayList(map.entrySet());
//3.1 list转map
List<RankVo> list1 = Lists.newArrayList();
Map<String, RankVo> appleMap = list1.stream().collect(Collectors.toMap(RankVo::getModelLabel, e -> e,(k1,k2)->k1));
Map<String, String> dataIdLogicIdMap = list1.stream().collect(Collectors.toMap(e -> e.getModelLabel(), e -> e.getName(), (k1, k2) -> k1));
//3.2 list统计后转map
List<DevicesInPlaceVo> devicesInPlaceVos = Lists.newArrayList();
Map<String, Integer> peceventcountTypeMap = new HashMap<>(16);
devicesInPlaceVos.stream().forEach(e -> peceventcountTypeMap.merge(e.getModelLabel(),e.getId().intValue(), Integer::sum));
//3.3 list分组转map
Map<String, List<RankVo>> rankMap = list1.stream().collect(Collectors.groupingBy(RankVo::getModelLabel));
//4.字段筛选,去重
List list3 = Lists.newArrayList();
//List<Long> deviceIds = list3.stream().map(e -> e.getMeasuredby()).distinct().collect(Collectors.toList());
//5.集合枚举
List<String> EVENT_LEVEL_IN_STRING = Stream.of("事故", "告警", "一般", "预警", "其他").collect(Collectors.toList());
//6.统计
List<Map> queryList = Lists.newArrayList();
int count = queryList.stream().mapToInt(e -> Integer.valueOf(e.get("count").toString())).sum();
//7.过滤,取均值
//最大值
//Double maxuabValue = allList.stream().filter(e -> e.get("uab") != null).mapToDouble(e -> (Double) e.get("uab")).max().getAsDouble();
// 最小值
//Double minuabValue = allList.stream().filter(e -> e.get("uab") != null).mapToDouble(e -> (Double) e.get("uab")).min().getAsDouble();
// 平均值
//Double averageuabValue = allList.stream().filter(e -> e.get("uab") != null).mapToDouble(e -> (Double) e.get("uab")).average().getAsDouble();
//8.查找任何一个
DevicesInPlaceVo dcbaseDeviceInfo = devicesInPlaceVos.stream().findAny().orElse(null);
//9.统计字段总数
List<PecEventCountVo> pecEventCountVos = Lists.newArrayList();
int totalCount = pecEventCountVos.stream().collect(Collectors.summingInt(vo -> (vo.getCount())));
}
}
本文地址:https://blog.csdn.net/t194978/article/details/109853774
上一篇: Java字符串常用的方法汇总