// 从集合获取流
list.stream();
// 数组获取流
Arrays.stream(arrays);
// 从值获取流
Stream.of("你", "我", "她");
String[] strings = {"珊瑚", "阳光", "细腻", "冷暖", "阳光"};
Arrays.stream(strings).filter(n -> n.startsWith("冷")).forEach(System.out::print);
Arrays.stream(strings).distinct().forEach(System.out::print);
// 截取前面两个单位:
Arrays.stream(strings).limit(2).forEach(System.out::print);
// 和上面的limit 相反,跳过前面两个
Arrays.stream(strings).skip(2).forEach(System.out::print);
Arrays.stream(strings).map(s -> s + "兮").forEach(System.out::println);
//Arrays.stream(strings).sorted((x, y) -> x.compareTo(y)).forEach(System.out::println);
Arrays.stream(strings).sorted(String::compareTo).forEach(System.out::println);
List list4 = Arrays.asList(1, 2, 3, 4, 1);
IntSummaryStatistics stats = list4.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("Highest number in List : " + stats.getMax());
System.out.println("Lowest number in List : " + stats.getMin());
System.out.println("Sum of all numbers : " + stats.getSum());
System.out.println("Average of all numbers : " + stats.getAverage());
Boolean result1 = Arrays.stream(strings).allMatch(s -> s.equals("java"));
System.out.println(result1);
Boolean reslut2 = Arrays.stream(strings).noneMatch(s -> s.equals("java"));
System.out.println(reslut2);
//随机读取一个
Optional getResult = Arrays.stream(strings).findAny();
System.out.println(getResult);
System.out.printf("获取Optional中的值:%s%n", getResult.get());
Optional是Java8新加入的一个容器,这个容器只存1个或0个元素,它用于防止出现NullpointException,它提供如下方法:
// 规约求和
String connectStrings = Arrays.stream(strings).reduce("", (x, y) -> x + y);
reduce中第一个参数是初始值,第二个参数是方法引用。
将流转换成集合和聚合元素。
// toList
List list2 = Arrays.stream(strings).collect(Collectors.toList());
// Get String by connected
String connectStrings2 = Arrays.stream(strings).collect(Collectors.joining(","));
Map> countryToLocaleList = Stream.of(Locale.getAvailableLocales())
.collect(Collectors.groupingBy(l -> l.getDisplayCountry()));
// predicate
Map> englishAndOtherLocales = Stream.of(Locale.getAvailableLocales()).collect(Collectors.groupingBy(l -> l.getDisplayLanguage().equalsIgnoreCase("English")));
// partitioningBy
Map> englishAndOtherLocales2 = Stream.of(Locale.getAvailableLocales()).collect(Collectors.partitioningBy(l -> l.getDisplayLanguage().equalsIgnoreCase("English")));
// 返回set,而不是list:
Map> countryToLocaleSet = Stream.of(Locale.getAvailableLocales()).collect(Collectors.groupingBy(l -> l.getDisplayCountry(), Collectors.toSet()));
// 返回value集合中元素的数量:
Map countryToLocaleCounts = Stream.of(Locale.getAvailableLocales()).collect(Collectors.groupingBy(l -> l.getDisplayCountry(), Collectors.counting()));
// 对value集合中的元素求和:
Map cityToPopulationSum = Stream.of(cities) .collect(Collectors.groupingBy(City::getName, Collectors.summingInt(City::getPopulation)));
// 对value的某一个字段求最大值,注意value是Optional的:
Map> cityToPopulationMax = Stream.of(cities).collect(Collectors.groupingBy(City::getName,Collectors.maxBy(Comparator.comparing(City::getPopulation))));
// 使用mapping对value的字段进行map处理:
Map> stateToNameMax = Stream.of(cities).collect(Collectors.groupingBy(City::getState, Collectors.mapping(City::getName,Collectors.maxBy(Comparator.comparing(String::length)))));
Map> stateToNameSet = Stream.of(cities).collect(Collectors.groupingBy(City::getState, Collectors.mapping(City::getName, Collectors.toSet())));
// 通过summarizingXXX获取统计结果:
Map stateToPopulationSummary = Stream.of(cities).collect(Collectors.groupingBy(City::getState, Collectors.summarizingInt(City::getPopulation)));
// 可以对结果作更复杂的处理,但是reducing()却并不常用:
Map stateToNameJoining = Stream.of(cities).collect(Collectors.groupingBy(City::getState, Collectors.reducing("", City::getName,(s, t) -> s.length() == 0 ? t : s + ", " + t)));
// 比如上例可以通过mapping达到同样的效果:
Map stateToNameJoining2 = Stream.of(cities).collect(Collectors.groupingBy(City::getState, Collectors.mapping(City::getName, Collectors.joining(", "))));