- stream的学习笔记
- 知识来源于: Java8 Stream:2万字20个实例,玩转集合的筛选、归约、分组、聚合
- 详细的图解请查看上文,该部分代码是方便自己查阅使用
package com.jd.code.day3;import java.util.*;import java.util.stream.Collectors;import java.util.stream.IntStream;import java.util.stream.Stream;public class StreamTest { public static List<Person> personList = new ArrayList<Person>(); public static void init() { personList.add(new Person("Tom", 8900, 23, "male", "New York")); personList.add(new Person("Jack", 7000, 25, "male", "Washington")); personList.add(new Person("Lily", 7800, 21, "female", "Washington")); personList.add(new Person("Anni", 8200, 24, "female", "New York")); personList.add(new Person("Owen", 9500, 25, "male", "New York")); personList.add(new Person("Alisa", 7900, 26, "female", "New York")); } public static void main(String[] args) { init(); } public static void test13(){ String[] arr1 = { "a", "b", "c", "d" }; String[] arr2 = { "d", "e", "f", "g" }; Stream<String> stream1 = Stream.of(arr1); Stream<String> stream2 = Stream.of(arr2); List<String> collect = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList()); System.out.println("collect = " + collect); List<Integer> collect1 = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList()); System.out.println("collect1 = " + collect1); List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(4).collect(Collectors.toList()); System.out.println("collect2 = " + collect2); } public static void test12() { List<String> collect = personList.stream() .sorted(Comparator.comparing(Person::getSalary)) .map(Person::getName) .collect(Collectors.toList()); System.out.println("collect = " + collect); List<Integer> collect1 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()) .map(Person::getSalary) .collect(Collectors.toList()); System.out.println("collect1 = " + collect1); List<String> collect2 = personList.stream() .sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)) .map(Person::getName) .collect(Collectors.toList()); System.out.println("collect2 = " + collect2); List<String> collect3 = personList.stream().sorted((p1, p2) -> { if (p1.getSalary() == p2.getSalary()) { return p2.getAge() - p1.getAge(); } else { return p2.getSalary() - p1.getSalary(); } }).map(Person::getName).collect(Collectors.toList()); System.out.println("collect3 = " + collect3); } public static void test11() { Integer collect = personList.stream() .collect(Collectors.reducing(0, Person::getSalary, (i, j) -> (i + j - 5000))); System.out.println("collect = " + collect); Optional<Integer> sum2 = personList.stream().map(Person::getSalary).reduce(Integer::sum); System.out.println("员工薪资总和:" + sum2.get()); } public static void test10() { String joinStr = personList.stream() .map(person -> person.getName()) .collect(Collectors.joining(", ")); System.out.println("joinStr = " + joinStr); List<String> list = Arrays.asList("A", "B", "C"); String str = list.stream().collect(Collectors.joining("-")); System.out.println("str = " + str); } public static void test09() { Map<Boolean, List<Person>> part = personList.stream().collect(Collectors.partitioningBy(person -> person.getSalary() > 8000)); System.out.println("part = " + part); Map<String, List<Person>> sexMap = personList.stream().collect(Collectors.groupingBy(Person::getSex)); System.out.println("sexPart = " + sexMap); Map<String, Map<String, List<Person>>> sexAndAreaGroup = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea))); System.out.println("sexAndAreaGroup = " + sexAndAreaGroup); } public static void test08() { long count = personList.stream().count(); System.out.println("count = " + count); Double average = personList.stream().collect(Collectors.averagingDouble(Person::getSalary)); System.out.println("average = " + average); Double sum = personList.stream().collect(Collectors.summingDouble(Person::getSalary)); System.out.println("sum = " + sum); Optional<Person> maxSalary = personList.stream().max(Comparator.comparing(Person::getSalary)); System.out.println("maxSalary.get() = " + maxSalary.get().getSalary()); } public static void test07() { List<Integer> list = Arrays.asList(1, 6, 3, 4, 6, 7, 9, 6, 20); List<Integer> list1 = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList()); System.out.println("list1 = " + list1); Set<Integer> set = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet()); System.out.println("set = " + set); Map<String, Person> map = personList.stream().filter(p -> p.getSalary() > 8000) .collect(Collectors.toMap(Person::getName, p -> p)); System.out.println("map = " + map); } public static void test06() { List<Integer> list = Arrays.asList(1, 3, 2, 8, 11, 4); Optional<Integer> sum = list.stream().reduce((x, y) -> x + y); Optional<Integer> sum2 = list.stream().reduce(Integer::sum); Integer sum3 = list.stream().reduce(0, Integer::sum); System.out.println("列表求和 = " + sum.get()); System.out.println("sum2.get() = " + sum2.get()); System.out.println("sum3 = " + sum3); Optional<Integer> product = list.stream().reduce((x, y) -> x * y); System.out.println("product.get() = " + product.get()); Optional<Integer> max = list.stream().reduce((x, y) -> x > y ? x : y); System.out.println("max.get() = " + max.get()); Optional<Integer> max1 = list.stream().max(Integer::compareTo); System.out.println("max1.get() = " + max1.get()); Integer max2 = list.stream().reduce(1, Integer::max); System.out.println("max2 = " + max2); Optional<Integer> salarySum = personList.stream().map(Person::getSalary) .reduce(Integer::sum); System.out.println("salarySum.get() = " + salarySum.get()); Optional<Integer> max3 = personList.stream().map(Person::getSalary).max(Integer::compareTo); System.out.println("max3.get() = " + max3.get()); } public static void test05() { String[] strArr = {"abcd", "bcdd", "defde", "fTr"}; List<String> stringList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList()); System.out.println("stringList = " + stringList); List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11); List<Integer> integerList = intList.stream().map(x -> x + 3).collect(Collectors.toList()); System.out.println("integerList = " + integerList); List<Integer> newPersonList = personList.stream().map(person -> person.getSalary() + 1000) .collect(Collectors.toList()); System.out.println("newPersonList = " + newPersonList); List<Person> personListNew = personList.stream().map(person -> { Person personNew = new Person(person.getName(), 0, 0, null, null); personNew.setSalary(person.getSalary() + 10000); return personNew; }).collect(Collectors.toList()); System.out.println("一次改动前:" + personList.get(0).getName() + "-->" + personList.get(0).getSalary()); System.out.println("一次改动后:" + personListNew.get(0).getName() + "-->" + personListNew.get(0).getSalary()); List<Person> personListNew2 = personList.stream().map(person -> { person.setSalary(person.getSalary() + 10000); return person; }).collect(Collectors.toList()); System.out.println("二次改动前:" + personList.get(0).getName() + "-->" + personListNew.get(0).getSalary()); System.out.println("二次改动后:" + personListNew2.get(0).getName() + "-->" + personListNew.get(0).getSalary()); List<String> list4 = Arrays.asList("m,k,l,a", "1,3,5,7"); List<String> listNew = list4.stream().flatMap(s -> { String[] split = s.split(","); Stream<String> s2 = Arrays.stream(split); return s2; }).collect(Collectors.toList()); System.out.println("处理前的集合: " + list4); System.out.println("处理后的集合: " + listNew); } public static void test04() { List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd"); Optional<String> max = list.stream().max(Comparator.comparing(String::length)); System.out.println("max.get() = " + max.get()); List<Integer> list1 = Arrays.asList(7, 6, 9, 4, 11, 6); Optional<Integer> max1 = list1.stream().max(Integer::compareTo); Optional<Integer> max2 = list1.stream().max(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }); System.out.println("max1.get() = " + max1.get()); System.out.println("max2.get() = " + max2.get()); Optional<Person> person = personList.stream().max(Comparator.comparing(Person::getSalary)); System.out.println("工资的最大值 = " + person.get().getSalary()); List<Integer> list2 = Arrays.asList(7, 6, 4, 8, 2, 11, 9); long count = list2.stream().filter(x -> x > 6).count(); System.out.println("count = " + count); } public static void test03() { List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1); list.stream().filter(x -> x > 7).forEach(System.out::println); List<String> newList = personList.stream() .filter(person -> person.getSalary() > 8000) .map(person -> person.getName()) .collect(Collectors.toList()); System.out.println("工资大于8000的员工姓名: " + newList); } public static void test02() { List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1); list.stream().filter(x -> x > 6).forEach(System.out::println); Optional<Integer> first = list.stream().filter(x -> x > 6).findFirst(); System.out.println("first.get() = " + first.get()); Optional<Integer> any = list.stream().filter(x -> x > 6).findAny(); System.out.println("any.get() = " + any.get()); boolean isMatch = list.stream().anyMatch(x -> x > 6); System.out.println("isMatch = " + isMatch); } public static void test01() { List<String> list = Arrays.asList("a", "b", "c"); Stream<String> stream = list.stream(); Stream<String> parallelStream = list.parallelStream(); int[] arr = {1, 2, 3, 4, 5}; IntStream stream1 = Arrays.stream(arr); Stream<Integer> stream2 = Stream.of(1, 2, 3, 4, 5, 6); Stream<Integer> stream3 = Stream.iterate(0, x -> x + 3).limit(4); stream3.forEach(System.out::println); Stream<Double> stream4 = Stream.generate(Math::random).limit(3); stream4.forEach(System.out::println); }}