字符串数组中是否有长度为1的字符串
有一个匹配到就返回true
public static void anyMatch() { boolean anyMatch = Stream.of("1", "2", "3", "11", "22", "33").anyMatch((str) -> str.length() == 1); System.out.println(anyMatch); }
字符串数组中是否有长度都小于3的字符串
public static void allMatch() { boolean allMatch = Stream.of("1", "2", "3", "11", "22", "33").allMatch((str) -> str.length() < 3); System.out.println(allMatch); }
字符串数组中是否有长度都没有大于3的字符串
public static void noneMatch() { boolean noneMatch = Stream.of("1", "2", "3", "11", "22", "33").noneMatch((str) -> str.length() > 3); System.out.println(noneMatch); }
anyMatch一个匹配就行,allMatch是全匹配,noneMatch和allMatch相反,全不匹配。anyMatch、 allMatch和noneMatch这三个操作都用到了短路,这就是大家熟悉的Java中&&和||运算符短路在流中的版本。
返回第一个元素
public static void findFirst() { Optional<String> first = Stream.of("1", "2", "3", "11", "22", "33").findFirst(); System.out.println(first.get()); }
获取第一个元素,有可能为空,返回值为Optional。
返回任意一个元素
public static void findAny() { Optional<String> findAny = Stream.of("1", "2", "3", "11", "22", "33").parallel().findAny(); System.out.println(findAny.get()); }
返回任意一个,有可能为空,返回值为Optional。
求和
public static void sum() { int sum = 0; int[] arr = new int[]{1, 2, 3, 4}; for (int num : arr) { sum += num; } System.out.println(sum); } public static void reduce1() { Integer reduce = Stream.of(1, 2, 3, 4).reduce(0, (a, b) -> a + b); //Integer reduce = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum); System.out.println(reduce); } public static void reduce2() { Optional<Integer> reduce = Stream.of(1, 2, 3, 4).reduce((a, b) -> a + b); System.out.println(reduce.get()); }
第一个方法,是1.7之前的。
第二个方法用的是reduce,redue第一个参数是初始值,这边是0,后面是BinaryOperator。
第三个方法,与第二个不同的是没有初始值,返回值是Optional。
大概流程如下:
求最大值和最小值
public static void max1() { Integer max = Stream.of(1, 2, 3, 4).reduce(0, Integer::max); System.out.println(max); } public static void max2() { Optional<Integer> max = Stream.of(1, 2, 3, 4).max(Integer::compareTo); System.out.println(max.get()); } public static void min() { Optional<Integer> min = Stream.of(1, 2, 3, 4).min(Integer::compareTo); System.out.println(min.get()); }
第一个方法是reduce求最大值,第二个是通过max求最大值,第三个是通过min求最小值。
下面例子要用的Person类,成员变量有name和sex,以及set、get、toString方法。
getPerson方法:获取List<Person>
public static List<Person> getPerson() { return Arrays.asList(new Person("张三", 1), new Person("李四", 1), new Person("王五", 2)); }
统计人员的个数
public static void counting() { List<Person> list = getPerson(); System.out.println(list.stream().collect(Collectors.counting())); System.out.println(list.stream().count()); }
求最大值和最小值
public static void maxBy() { Optional<String> maxBy = Arrays.stream(new String[]{"a", "c", "d"}).collect(Collectors.maxBy(String::compareToIgnoreCase)); System.out.println(maxBy.get()); } public static void minBy() { Optional<String> minBy = Arrays.stream(new String[]{"a", "c", "d"}).collect(Collectors.minBy(String::compareToIgnoreCase)); System.out.println(minBy.get()); }
求和及求平均值
public static void summingInt() { int sum = Arrays.stream(new Integer[]{1, 2, 3, 4}).collect(Collectors.summingInt(i -> i)); System.out.println(sum); //summingDouble、summingLong } public static void averagingInt() { double avg = Arrays.stream(new Integer[]{1, 2, 3}).collect(Collectors.averagingInt(i -> i)); System.out.println(avg); //averagingDouble、averagingLong }
summingDouble、summingLong类似于summingInt。
averagingDouble、averagingLong类似于averagingInt。
求总数、最大值和最小值、求和和平均值
public static void summarizingInt() { IntSummaryStatistics intSummaryStatistics = Arrays.stream(new Integer[]{1, 2, 3}).collect(Collectors.summarizingInt(i -> i)); System.out.println(intSummaryStatistics); //summarizingDouble、summarizingLong }
运行结果如下:
summarizingDouble、summarizingLong类似于summarizingInt。
拼接数组的字符串
public static void joining() { String joining1 = Arrays.stream(new String[]{"a", "c", "d"}).collect(Collectors.joining()); String joining2 = Arrays.stream(new String[]{"a", "c", "d"}).collect(Collectors.joining(",")); String joining3 = Arrays.stream(new String[]{"a", "c", "d"}).collect(Collectors.joining(",", "prefix", "suffix")); System.out.println(joining1); System.out.println(joining2); System.out.println(joining3); }
运行结果如下:
参数有分隔符、前缀、后缀。
对数组进行计算
public static void reducing() { Integer reducing1 = Stream.of(1, 2, 3, 4).collect(Collectors.reducing(0, (a, b) -> a + b)); Optional<Integer> reducing2 = Stream.of(1, 2, 3, 4).collect(Collectors.reducing((a, b) -> a + b)); Integer reducing3 = Stream.of(1, 2, 3, 4).collect(Collectors.reducing(0, i -> i * i, (a, b) -> a + b)); System.out.println(reducing1); System.out.println(reducing2.get()); System.out.println(reducing3); }
运行结果如下:
reducing1是以0为初始值,对数组进行累加。
reducing2没有初始值,对数组进行累加,返回的是Optional。
reducing3,把数组的数字转换为平方数,再进行累加。
通过性别分组
public static void groupingBy() { List<Person> list = getPerson(); Map<Integer, List<Person>> groupingBy = list.stream().collect(Collectors.groupingBy(Person::getSex)); System.out.println(groupingBy); }
运行结果如下:
groupingByConcurrent类似于groupingBy,只是返回的是ConcurrentMap
通过性别分区
public static void partitioningBy() { List<Person> list = getPerson(); Map<Boolean, List<Person>> partitioningBy = list.stream().collect(Collectors.partitioningBy(person -> 1 == person.getSex())); System.out.println(partitioningBy); }
运行结果如下:
输出list
public static void toList() { List<Person> list = getPerson(); List<Person> toList = list.stream().collect(Collectors.toList()); System.out.println(toList); }
输出set
public static void toSet() { List<Person> list = getPerson(); Set<Person> toSet = list.stream().collect(Collectors.toSet()); System.out.println(toSet); }
把Person的name作为key,sex作为value,输出map
public static void toMap() { List<Person> list = getPerson(); Map<String, Integer> toMap = list.stream().collect(Collectors.toMap((person) -> person.getName(), (person) -> person.getSex())); System.out.println(toMap); }
toConcurrentMap类似于toMap,只是返回的是ConcurrentMap
获取到Person后再取toString的集合
public static void collectingAndThen() { List<Person> list = getPerson(); String str = list.stream().collect(Collectors.collectingAndThen(Collectors.toList(), persons -> persons.toString())); System.out.println(str); }
先把Person映射成name,再输出集合
public static void mapping() { List<Person> list = getPerson(); List<String> collect = list.stream().collect(Collectors.mapping(person -> person.getName(), Collectors.toList())); System.out.println(collect); }