Stream流
flatMapToInt()函数,具有将字符串解析为整数的操作
List<String> list = Arrays.asList("1", "2", "3", "4", "5");
list.stream().flatMapToInt(num-> IntStream.of(Integer.parseInt(num))).forEach(System.out::println);
List<String> asList = Arrays.asList("GeeksforGeeks", "Geeks");
int[] ints = asList.stream().flatMapToInt(s -> IntStream.of(s.length())).toArray();
for (int anInt : ints) {
System.out.println("anInt = " + anInt);
}
distinct()函数 去重
List<Integer> integers = Arrays.asList(1, 1, 2, 3, 4, 2);
List<Integer> collect = integers.stream().distinct().collect(Collectors.toList());
sorted() 排序
List<Integer> integers = Arrays.asList(4,1, 1, 2, 3, 2);
integers.stream().distinct().sorted().collect(Collectors.toList()).forEach(System.out::println);
List<Integer> integers = Arrays.asList(4,1, 1, 2, 3, 2);
integers.stream().distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList()).forEach(System.out::println);
menuDTOS.stream().sorted(Comparator.comparing(MenuDTO::getId)).collect(Collectors.toList()).forEach(System.out::println);
menuDTOS.stream().sorted(Comparator.comparing(MenuDTO::getId).reversed()).collect(Collectors.toList()).forEach(System.out::println);
menuDTOS.stream().sorted(Comparator.comparingInt(MenuDTO::getOrderNum).reversed()).forEach(System.out::println);
limit(long maxSize)截取
list.stream().sorted().limit(3).collect(Collectors.toList()).forEach(System.out::println);
skip() 跳过元素
list.stream().sorted().skip(3).collect(Collectors.toList()).forEach(System.out::println);
forEachOrdered(Consumer<? super T> action) 顺序循环
List<Integer> list = Arrays.asList(9, 2, 2, 6, 8, 10);
list.stream().forEachOrdered(System.out::println);
toArray(IntFunction<A[]> generator)转数组
List<Integer> list = Arrays.asList(9, 2, 2, 6, 8, 10);
Integer[] integers = list.stream().toArray(Integer[]::new);
IntFunction<Date[]> intFunction = Date[]::new;
Date[] dates = intFunction.apply(10);
Map<Date,String> map = new HashMap();
Date[] dates = map.keySet().stream().toArray(Date[]::new);
Date[] dates = map.keySet().stream().toArray(size -> {
return new Date[size];});
reduce()计算
List<Integer> list = Arrays.asList(9, 2, 2, 6, 8, 10);
Integer integer = list.stream().reduce(Integer::sum).get();
Integer integer1 = list.stream().reduce((a, b) -> a + b).get();
Integer reduce = list.stream().reduce(1000, (a, b) -> a += b);
System.out.println("reduce = " + reduce);
ArrayList<Integer> newList = new ArrayList<>();
ArrayList<Integer> reduce1 = list.stream().reduce(newList, (acc, item) -> {
newList.add(item);
return acc;
}, (acc, item) -> null);
reduce1.forEach(System.out::println);
参考文档:https://blog.csdn.net/icarusliu/article/details/79504602
stream流中collect用法
Collectors.groupingBy(People::getName)
Integer collect1 = menuDTOS.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(menuDTO -> menuDTO.getParent()), Map::size));
System.out.println("collect1 = " + collect1);
Map<String, Long> collect = menuDTOS.stream().collect(Collectors.groupingBy(MenuDTO::getParent, Collectors.counting()));
for (Map.Entry<String, Long> stringLongEntry : collect.entrySet()) {
System.out.println("stringLongEntry = " + stringLongEntry.getKey() +"::::"+stringLongEntry.getValue());}
Map<String, List<MenuDTO>> collect2 = menuDTOS.stream().collect(Collectors.groupingBy(MenuDTO::getParent));
for (Map.Entry<String, List<MenuDTO>> stringListEntry : collect2.entrySet()) {
System.out.println(stringListEntry.getKey()+"=" + stringListEntry.getValue());
}
Map<String, Optional<MenuDTO>> collect3 = menuDTOS.stream().collect(Collectors.groupingBy(MenuDTO::getParent, Collectors.maxBy(Comparator.comparing(MenuDTO::getId))));
for (Map.Entry<String, Optional<MenuDTO>> stringOptionalEntry : collect3.entrySet()) {
System.out.printf(stringOptionalEntry.getKey() +"=" + stringOptionalEntry.getValue());
}
Map<String, MenuDTO> collect2 = menuDTOS.stream().collect(Collectors.toMap(MenuDTO::getId, Function.identity()));
for (Map.Entry<String, MenuDTO> entry : collect2.entrySet()) {
System.out.println(entry.getKey() + ":::" + entry.getValue());
}
参考文档:
https://blog.csdn.net/qq_43212582/article/details/109312598
max(Comparator<? super T> comparator); 取最大值
List<Integer> integers = Arrays.asList(4,1, 1, 2, 3, 2);
Integer integer = integers.stream().max((a, b) -> {
if (a > b) {
return 1;
} else {
return -1;
}
}).get();
System.out.println("integer = " + integer);
}
min(Comparator<? super T> comparator) 取最小的值
List<Integer> integers = Arrays.asList(4,1, 1, 2, 3, 2);
Integer integer1 = integers.stream().distinct().min((a, b) -> {
if (a > b) {
return 1;
} else {
return 0;
}
}).get();
System.out.println("integer1 = " + integer1);
findFirst() 获取流中的第一个元素
Optional<Integer> first = integers.stream().findFirst();
if (first.isPresent()){
System.out.println("integer2 = " + first.get());
}
Integer integer3 = integers.stream().findFirst().orElse(null);
Optional<Integer> any = integers.stream().findFirst();
any.ifPresent(value -> System.out.println("present = " + value));
findAny()寻找List中符合要求的数据
Integer integer2 = integers.stream().filter(s -> s == 5).findAny().orElse(null);
System.out.println("integer2 = " + integer2);
Integer integer3 = integers.stream().findAny().orElse(null);
System.out.println("integer3 = " + integer3);
builder()构建流
Stream<Object> build = Stream.builder().add(1).add(2).build();
build.forEach(System.out::println);
anyMatch(Predicate<? super T> predicate) 匹配
boolean anyMatch = integers.stream().anyMatch(s -> s == 4);
System.out.println("anyMatch = " + anyMatch);
boolean allMatch = integers.stream().allMatch(s -> s == 4);
System.out.println("allMatch = " + allMatch);
boolean noneMatch = integers.stream().noneMatch(s -> s == 6);
System.out.println("noneMatch = " + noneMatch);
boolean b = menuDTOS.stream().anyMatch(menuDTO -> menuDTO.getId().equals("100"));
System.out.println("b = " + b);
concat(Stream a, Stream b) 两个流放在一起
Stream<Integer> integerStream = Stream.of(1, 2, 3);
Stream<Integer> integerStream1 = Stream.of(2, 2, 2);
List<Integer> collect = Stream.concat(integerStream, integerStream1).collect(Collectors.toList());
System.out.println("collect = " + collect);
List<Integer> collect = Stream.of(1, 2, 3, 4, 5, 6).collect(Collectors.toList());
List<Integer> collect1 = Stream.of(8, 9, 10, 11, 12).collect(Collectors.toList());
List<Integer> collect2 = Stream.of(collect, collect1).flatMap(test -> test.stream()).collect(Collectors.toList());
System.out.println("collect2 = " + collect2);
iterate(final int seed, final IntUnaryOperator f)迭代生成无限整数流
IntStream limit = IntStream.iterate(2, n -> n * 3).limit(3);
limit.forEach(System.out::println);
Collectors类的使用
joining对Stream的结果字符串拼接
String collect2 = list.stream().collect(Collectors.joining());
System.out.println("collect2 = " + collect2);
String collect3 = list.stream().collect(Collectors.joining("!"));
System.out.println("collect3 = " + collect3);
String collect4 = list.stream().collect(Collectors.joining("@", "start--", "end--"));
System.out.println("collect4 = " + collect4);
toCollection(Supplier collectionFactory) 收集为指定的集合
List<String> list = Arrays.asList("java", "python", "C++","php","java");
List<String> collect = list.stream().collect(Collectors.toCollection(LinkedList::new));
System.out.println("collect = " + collect);
TreeSet<String> collect1 = list.stream().collect(Collectors.toCollection(TreeSet::new));
System.out.println("collect1 = " + collect1);
HashSet<String> collect5 = list.stream().collect(Collectors.toCollection(HashSet::new));
System.out.println("collect5 = " + collect5);
Collectors.groupingBy用法
Map<String, List<MenuDTO>> collect = menuDTOS.stream().collect(Collectors.groupingBy(MenuDTO::getId));
Set<Map.Entry<String, List<MenuDTO>>> entries = collect.entrySet();
for (Map.Entry<String, List<MenuDTO>> entry : entries) {
System.out.println(entry.getKey()+"===" + entry.getValue());
}
Map<String, List<MenuDTO>> collect1 = menuDTOS.stream().collect(Collectors.groupingBy(item -> item.getId() + "-" + item.getMenuName()));
for (Map.Entry<String, List<MenuDTO>> stringListEntry : collect1.entrySet()) {
System.out.println(stringListEntry.getKey() + "===" + stringListEntry.getValue());
}
Map<? extends Serializable, List<MenuDTO>> collect2 = menuDTOS.stream().collect(Collectors.groupingBy(item -> {
if (item.getOrderNum() > 2) {
return 2;
} else {
return "other";
}
}));
for (Map.Entry<? extends Serializable, List<MenuDTO>> listEntry : collect2.entrySet()) {
System.out.println(listEntry.getKey() + "==" + listEntry.getValue());
}
Map<String, Long> collect = menuDTOS.stream().collect(Collectors.groupingBy(MenuDTO::getParent, Collectors.counting()));
for (Map.Entry<String, Long> stringLongEntry : collect.entrySet()) {
System.out.println(stringLongEntry.getKey() + "===" + stringLongEntry.getValue());
}
Collectors类的研究使用
https://zhuanlan.zhihu.com/p/82508008 递归使用
集合:
LinkedList
CopyOnWriteArrayList
TreeSet