流操作
拼接
Collectors.joining(",")
Collectors.joining(",", "[", "]")
统计
Collectors.counting()
计数
Collectors.summingLong(String::length)
数据源
数据源
1.如果是数组或者list<基础类型>
String[] listValue = {"i", "love", "leetcode", "i", "love", "coding", "code", "test", "auto", "shit", "auto"};
Function.identity() 数据源,一个一个循环传递"i"
Map<String, Long> mapmm= Arrays.stream(listValue).collect(Collectors.groupingBy(Function.identity(),Collectors.summingLong(String::length)));
2.如果是List<对象>
List<对象> list=new List<对象>();
对象::属性
流收集器操作方式
groupingBy分组
Collectors.groupingBy(Function.identity(),Collectors.joining(","))
Collectors.mapping拼接
String mapmm= Arrays.stream(listValue).collect(Collectors.mapping(Function.identity(), Collectors.joining(",", "[", "]")));
Collectors.toMap转Map
stream().collect(Collectors.toMap(Person::getName, p -> p));
数据转换
Collectors.toCollection(Supplier<C> collectionFactory)
//直接创建treeSet数据 [1, 3, 4]
TreeSet<Integer> collect3= Stream.of(1, 3, 4).collect(Collectors.toCollection(TreeSet::new));
List<String> list = Arrays.asList("java", "python", "C++","php","java");
//用LinkedList收集
List<String> linkedListResult = list.stream().collect(Collectors.toCollection(LinkedList::new));
linkedListResult.forEach(System.out::println);
//用CopyOnWriteArrayList收集
List<String> copyOnWriteArrayListResult = list.stream().collect(Collectors.toCollection(CopyOnWriteArrayList::new));
copyOnWriteArrayListResult.forEach(System.out::println);
//用TreeSet收集
TreeSet<String> treeSetResult = list.stream().collect(Collectors.toCollection(TreeSet::new));
treeSetResult.forEach(System.out::println);
流转list
Collectors.toList()
List<String> nameCollect = platformUsers.stream().map(PlatformUser::getName).collect(Collectors.toList());
变量转list
String teamId=“”;
Collections.singletonList(teamId)
变量转map
Collections.singletonMap(key,value)
map数据过滤方式跟list过滤方式不一样
这个过滤true表示要元素
Map<Integer, Integer> dataMap = MapUtil.filter(rootMap, entry -> Constants.INT1.equals(entry.getValue()));
创建一个步长为几的流
这个方法用于生成一个顺序排列的整数流,包含起始值但不包含结束值
可以从下面的例子中发IntStream.range,可以创造一个流,来执行你需要的逻辑
dataMap.forEach((teamId, teamList) -> IntStream.range(0, teamList.size())
.filter(i -> teamList.get(i).getId().equals(teamId)).findAny().ifPresent(i -> levelMap.put(teamId, i + 1)));