jdk1.8后开始支持的stream流,最近用了流之后,都快不想写for循环了,流写起来实在是太方便了。
这里记录下我平时常用的几个操作:
List<Map<String, Object>> 根据map中key为value的值进行排序
list.sort(Comparator.comparingLong(m -> Long.parseLong(m.get("value").toString())));
List<实体>转List<String>
List<String> strList = list.stream().map(实体::getValue).collect(Collectors.toList())
List<Long>转 Map<Long, Long>
Map<Long, Long> map = list.stream().collect(Collectors.toMap(Function.identity(), Function.identity()));
List<Map<String, Object>>转Map
Map map = list.stream().collect(Collectors.toMap(m -> m.get("index"), m -> m.get("value")));
List<Map<String, Object>>转Set
Set set = list.stream().map(m -> m.get("value")).collect(Collectors.toSet());