字符串數組中是否有長度爲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中&&和||運算符短路在流中的版本。app
返回第一個元素
public static void findFirst() { Optional<String> first = Stream.of("1", "2", "3", "11", "22", "33").findFirst(); System.out.println(first.get()); }
獲取第一個元素,有可能爲空,返回值爲Optional。spa
返回任意一個元素
public static void findAny() { Optional<String> findAny = Stream.of("1", "2", "3", "11", "22", "33").parallel().findAny(); System.out.println(findAny.get()); }
返回任意一個,有可能爲空,返回值爲Optional。code
求和
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。
大概流程以下:blog
求最大值和最小值
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求最小值。ci
下面例子要用的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。get
求總數、最大值和最小值、求和和平均值
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。it
拼接數組的字符串
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); }
運行結果以下:
參數有分隔符、前綴、後綴。io
對數組進行計算
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); }