Stream流

Stream流_第1张图片

1、Filter 过滤

    list.stream().filter(e -> e < 8).forEach(System.out::println);
    list.stream().filter(e ->e>3).forEach(System.out::println);
    list.stream().filter(e -> e>8).findFirst();

 

stringCollection
    .stream()
    .filter((s) -> s.startsWith("a"))
    .forEach(System.out::println);

// "aaa2", "aaa1"

Stream流_第2张图片

 

2、Sorted 排序

stringCollection
    .stream()
    .sorted()
    .filter((s) -> s.startsWith("a"))
    .forEach(System.out::println);

// "aaa1", "aaa2"

3、Map 转换

 

   List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);


        double avg = list.stream().mapToDouble(Number::doubleValue).average().getAsDouble();
        double sum9 = list.stream().mapToDouble(Number::doubleValue).sum();
        double max = list.stream().mapToInt(Number::intValue).max().getAsInt();
        System.out.println("平均"+avg+"sum"+sum9+"max"+max);


  List list = Arrays.asList(1, 3, 5, 7, 9, 11);
        List collect1 = list.stream().map(e -> e + 3).collect(Collectors.toList());
        System.out.println("加三"+collect1);


String[] strArr = {"abcd","dfsd","sdfs","123"};
        List collect = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());
        System.out.println("大写"+collect);

stringCollection
    .stream()
    .map(String::toUpperCase)
    .sorted((a, b) -> b.compareTo(a))
    .forEach(System.out::println);

// "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1"

4、Match 匹配

 

5、Reduce

public class P1 {
    public static void main(String[] args) {
        List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);

        Optional sum = list.stream().reduce((x, y) -> x + y);
        Optional sum1 = list.stream().reduce(Integer::sum);
        Integer sum2 = list.stream().reduce(0, Integer::sum);

        Optional re1 = list.stream().reduce((x, y) -> x * y);

        Optional re2 = list.stream().reduce((x, y) -> x > y ? x : y);
        Integer re3 = list.stream().reduce(1,Integer::max);
        System.out.println("和"+sum.get());
        System.out.println("和"+sum1.get());
        System.out.println("和"+sum2);
        System.out.println(re3);

6、count计数

// 先对 list 中字符串开头为 b 进行过滤,让后统计数量
long startsWithB =
    stringCollection
        .stream()
        .filter((s) -> s.startsWith("b"))
        .count();

System.out.println(startsWithB);    // 3

 7、collect

 public static void main(String[] args) {
        /*
        给定一个字符串数组,使用 Stream 把所有字符串拼接成一个字符串。
         */
        String[] arr = {"a", "b", "c"};
        String s = Arrays.stream(arr).collect(Collectors.joining());
        System.out.println(s);

    }

8、综合

package com.kfm.it.Collection.Stream1;

import com.kfm.it.Strictice.Array;

import java.util.Arrays;
import java.util.List;
/*
有一个整数集合,分别完成以下操作:
- 所有元素之和
- 最大值
- 最小值
 */
public class PP3 {
    public static void main(String[] args) {
        List list = Arrays.asList(1, 2, 3, 4, 5);


        int max = list.stream()
                .mapToInt(e -> e)
                .max()
                .getAsInt();



        Integer sum1 = list.stream().reduce(Integer::sum).get();
        Integer sum2 = list.stream().reduce(0, Integer::sum);
        Integer sum3 = list.stream().reduce((x, y) -> x + y).get();

        Integer max1 = list.stream().reduce(Integer::max).get();
        Integer max2 = list.stream().reduce((x, y) -> x > y ? x : y).get();
        Integer max3 = list.stream().reduce(0, Integer::max);

        Integer min1 = list.stream().reduce(Integer::min).get();

        System.out.println("和"+sum1);
        System.out.println("和"+sum2);
        System.out.println("和"+sum3);


        System.out.println("最大值"+max1);
        System.out.println("最大值"+max2);
        System.out.println("最大值"+max3);

        System.out.println("最小值"+min1);



    }
}
package com.kfm.it.Collection.Stream1;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PP4 {
    public static void main(String[] args) {
        /*
        有一个员工类Employee,里面有name、age和salary字段。
        请通过 Stream 对多个Employee对象按salary排序,然后取前3个输出。

         */
        ArrayList list = new ArrayList<>();
        list.add(new Employee("张三1", 20, 4334.5));
        list.add(new Employee("张三2", 22, 4454.5));
        list.add(new Employee("张三3", 23, 4488.5));
        list.add(new Employee("张三4", 24, 4754.5));
        Employee e1 = new Employee("sf", 33, 4445.9);
        System.out.println(list.size());

//总数
        Long count = list.stream().collect(Collectors.counting());
        System.out.println("总数" + count);
//平均值
        Double avg = list.stream().collect(Collectors.averagingDouble(Employee::getSalay));
        System.out.println("平均" + avg);
        //和
        Double sum = list.stream().collect(Collectors.summingDouble(Employee::getSalay));
        System.out.println("工资和" + sum);
//升序
        List sort = list.stream().sorted(Comparator.comparing(Employee::getSalay)).map(Employee::getNeme).collect(Collectors.toList());
        //倒叙
        List sort1 = list.stream().sorted(Comparator.comparing(Employee::getSalay).reversed()).map(Employee::getNeme).limit(3).collect(Collectors.toList());
        
        System.out.println(sort);
        System.out.println(sort1);

    }
}
package com.kfm.it.Collection.Stream1;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PP5 {
    public static void main(String[] args) {
        /*
        ## 平均值
有一个整数集合,求每个元素平方的平均值。
         */
        List list = Arrays.asList(1,2,3,4,5,6);
        double asDouble = list.stream().mapToInt(e -> e * e).average().getAsDouble();
        System.out.println(asDouble);

        
    }
}

你可能感兴趣的:(java,前端,服务器)