1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| package com.nwa;
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream;
public class Stream2 { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); 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"));
fun1(); fun2(); fun3(); fun4(personList); fun5(personList); fun6(); fun7(personList); }
public static void fun1() { List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
list.stream().filter(x -> x > 6).forEach(System.out::println); Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst(); Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny(); boolean anyMatch = list.stream().anyMatch(x -> x > 6);
System.out.println("匹配第一个值:" + findFirst.get()); System.out.println("匹配任意一个值:" + findAny.get()); System.out.println("是否存在大于6的值:" + anyMatch); }
public static void fun2() { 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()); }
public static void fun3() { List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6); Optional<Integer> max = list.stream().max(Integer::compareTo); Optional<Integer> min = list.stream().max((o1, o2) -> o2 - o1); System.out.println("自然排序的最大值:" + max.get()); System.out.println("自定义排序的最小值:" + min.get()); }
public static void fun4(List<Person> personList) { Optional<Person> max = personList.stream().max(Comparator.comparingInt(Person::getSalary)); System.out.println("员工薪资最大值:" + max.get().getSalary());
List<Integer> list = Arrays.asList(7, 6, 4, 8, 2, 11, 9); long count = list.stream().filter(x -> x > 6).count(); System.out.println("list中大于6的元素个数:" + count); }
public static void fun5(List<Person> personList) { String[] strArr = {"abcd", "bcdd", "defde", "fTr"}; List<String> strList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList()); List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11); List<Integer> intListNew = intList.stream().map(x -> x + 3).collect(Collectors.toList()); System.out.println("每个元素大写:" + strList); System.out.println("每个元素+3:" + intListNew);
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).getSalary()); System.out.println("改动后:" + personListNew.get(0).getSalary());
List<Person> personListNew2 = personList.stream().map(person -> { person.setSalary(person.getSalary() + 10000); return person; }).collect(Collectors.toList()); }
public static void fun6() { List<String> list = Arrays.asList("m-k-l-a", "1,3,5,7"); List<String> listNew = list.stream().flatMap(s -> { String[] split = s.split("-"); return Arrays.stream(split); }).collect(Collectors.toList());
System.out.println("处理前:" + list); System.out.println("处理后:" + listNew); }
public static void fun7(List<Person> personList) { Optional<Integer> sumSalary = personList.stream().map(Person::getSalary).reduce(Integer::sum); Integer sumSalary2 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(), Integer::sum);
Integer maxSalary = personList.stream().reduce(0, (max, p) -> max > p.getSalary() ? max : p.getSalary(), Integer::max); Integer maxSalary2 = personList.stream().map(Person::getSalary).reduce(Integer::max).get();
System.out.println("工资之和:" + sumSalary.get() + "," + sumSalary2); System.out.println("最高工资:" + maxSalary + "," + maxSalary2); } }
|