stream 用法(list转map)(string转list)(list转map)

List 转化为Map

Map totalMap = ListMap.stream().collect(
Collectors.toMap(x -> x.get(key).toString(),
x -> Integer.valueOf(x.get(Key).toString())));

key 为map中的 key值 ,上述表达式的意思是 获取ListMap中Map的key值转换为map中的key 和 value

String转换为List

String s = “1,2,3”;
List collect = Arrays.stream(StringUtils.split(s, “,”)).map(s1 -> Integer.valueOf(s1.trim())).collect(Collectors.toList());

算和

int count = freeChannels.stream().mapToInt(m -> Integer.valueOf(m.get(“count”).toString())).sum();

计算map 中count 的和

map 转list

根据map的key值排序
List list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

根据map的value值排序
List list = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

根据map的value值排序
List list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

过滤list中的一些条件形成新的list

list = List.stream().filter(sourceCarModelId -> 过滤条件).collect(Collectors.toList());

你可能感兴趣的:(java)