Java8:2万字20个实例玩转集合—筛选、归约、分组、聚合( 三 )


  • map:接收一个函数作为参数 , 该函数会被应用到每个元素上 , 并将其映射成一个新的元素 。
  • flatMap:接收一个函数作为参数 , 将流中的每个值都换成另一个流 , 然后把所有流连接成一个流 。

Java8:2万字20个实例玩转集合—筛选、归约、分组、聚合文章插图
Java8:2万字20个实例玩转集合—筛选、归约、分组、聚合文章插图
案例一:英文字符串数组的元素全部改为大写 。 整数数组每个元素+3 。
public class StreamTest { public static void main(String[] args) {String[] strArr = { "abcd", "bcdd", "defde", "fTr" };List strList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());List intList = Arrays.asList(1, 3, 5, 7, 9, 11);List intListNew = intList.stream().map(x -> x + 3).collect(Collectors.toList());System.out.println("每个元素大写:" + strList);System.out.println("每个元素+3:" + intListNew); }}输出结果:
每个元素大写:[ABCD, BCDD, DEFDE, FTR]每个元素+3:[4, 6, 8, 10, 12, 14]
案例二:将员工的薪资全部增加1000 。
public class StreamTest { public static void main(String[] args) {List 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"));// 不改变原来员工集合的方式List 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 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()); }}输出结果:
一次改动前:Tom–>8900一次改动后:Tom–>18900二次改动前:Tom–>18900二次改动后:Tom–>18900
案例三:将两个字符数组合并成一个新的字符数组 。
public class StreamTest { public static void main(String[] args) {List list = Arrays.asList("m,k,l,a", "1,3,5,7");List listNew = list.stream().flatMap(s -> {// 将每个元素转换成一个streamString[] split = s.split(",");Stream s2 = Arrays.stream(split);return s2;}).collect(Collectors.toList());System.out.println("处理前的集合:" + list);System.out.println("处理后的集合:" + listNew); }}输出结果:
处理前的集合:[m-k-l-a, 1-3-5]处理后的集合:[m, k, l, a, 1, 3, 5]
3.5 归约(reduce)归约 , 也称缩减 , 顾名思义 , 是把一个流缩减成一个值 , 能实现对集合求和、求乘积和求最值操作 。
Java8:2万字20个实例玩转集合—筛选、归约、分组、聚合文章插图
案例一:求Integer集合的元素之和、乘积和最大值 。
public class StreamTest { public static void main(String[] args) {List list = Arrays.asList(1, 3, 2, 8, 11, 4);// 求和方式1Optional sum = list.stream().reduce((x, y) -> x + y);// 求和方式2Optional sum2 = list.stream().reduce(Integer::sum);// 求和方式3Integer sum3 = list.stream().reduce(0, Integer::sum);// 求乘积Optional product = list.stream().reduce((x, y) -> x * y);// 求最大值方式1Optional max = list.stream().reduce((x, y) -> x > y ? x : y);// 求最大值写法2Integer max2 = list.stream().reduce(1, Integer::max);System.out.println("list求和:" + sum.get() + "," + sum2.get() + "," + sum3);System.out.println("list求积:" + product.get());System.out.println("list求和:" + max.get() + "," + max2); }}输出结果:
list求和:29,29,29list求积:2112list求和:11,11
案例二:求所有员工的工资之和和最高工资 。
public class StreamTest { public static void main(String[] args) {List 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"));// 求工资之和方式1:Optional