Java8新特性Stream流

根据数据源所产生的元素序列,关注数据的计算

注意:
    1.流本身不能存储数据
    2.流不能修改数据源中的数据
    3.流是一次性的流,流是式操作的每一步都会返回一个持有结果的新流
    4.延迟执行|惰性加载 : 当不进行终止行为时候,不会执行流式中间操作
过程:
   1.获取|创建stream
   2.流式中间操作
   3.终止行为(返回的结果不在是stream)

1.获取|创建stream

public class Class01_Stream {
    public static void main(String[] args) {
        //1.Collection--> stream
        List<Integer> list = new ArrayList<>();
        //顺序流
        Stream<Integer> stream = list.stream();
        //并行流
        Stream<Integer> stream1 = list.stream();

        System.out.println(stream1);

        //2)Arrays.stream(array)
        Stream stream2 = Arrays.stream(new String[]{"1"});
        System.out.println(stream2);

        //3)Stream.of(数据1,数据2,数据3...)
        Stream stream3 = Stream.of("a","b");
        stream3.forEach(System.out::println);

        //Stream.of(数组)
        Stream stream4 = Stream.of(new String[]{});
        stream4.forEach(System.out::println);
    }
}

2.流式中间操作各种方法

/*
    中间操作
        筛选和切片
            filter-接收Lambda,从流中排除某些元素
            limit-截断流,使其元素不超过给定数量
            skip-跳过元素,返回一个扔掉了前n个元素的流,若流中元素不足n个,则返回一个空流。
            distinct-筛选,通过流所生产元素的hashCode()和equals()去除重复元素

     排序
        sorted(Comparable)-自然排序
        sorted(Comparator)-定制排序
 */
public class Class02_Stream {
    static List<Employee> emps = Arrays.asList(
            new Employee(102,"张三", 18, 3333.33),
            new Employee(101,"李四", 38, 4444.44),
            new Employee(104,"王五", 50, 5555.55),
            new Employee(104,"王五", 50, 5555.55),
            new Employee(103,"赵六", 16, 6666.66),
            new Employee(105,"田七", 28, 7777.77)
    );
    public static void main(String[] args) {
        //1.获取流
        Stream<Employee> stream = emps.stream();
        //2.中间操作
        Stream<Employee> stream1 = stream
                .filter(e->{
                    return e.getAge()>=18;
                })
                .distinct()  //去重
                //.limit(3)
                //.skip(1)
                //.sorted();  //默认根据内部比较器
                .sorted((x,y)->Double.compare(y.getPrice(), x.getPrice()));



        //3.终止行为
        stream1.forEach(System.out::println);
    }
}
/*
    中间操作
        映射:
            map : 将stream操作的每一个元素都作用与实参传递的函数,映射一个结果,返回操作结果的流  ---> *****
            flatMap : 将stream操作的每一个元素都作用与实参传递的函数,映射一个结果,结果必须为一个流,最终返回所有结果流结合以后的一个流
 */
public class Class003_map {
    static List<Employee> emps = Arrays.asList(
            new Employee(102,"张三", 18, 3333.33),
            new Employee(101,"李四", 38, 4444.44),
            new Employee(104,"王五", 50, 5555.55),
            new Employee(104,"王五", 50, 5555.55),
            new Employee(103,"赵六", 16, 6666.66),
            new Employee(105,"田七", 28, 7777.77)
    );

    public static void main(String[] args) {
        //1.获取流
        Stream<Employee> stream = emps.stream();

        //2.中间操作
        //1) 获取员工姓名的流
        Stream<String> stream1 = stream
                .map(employee -> employee.getName())
                .distinct();


        List<String> list = List.of("aaa","bbb","ccc");

        Stream<Character> stream2 = list.stream()
                //.map(Class003_map::getCharacterStream);  //[[a,a,a],[b,b,b],[c,c,c]]
                .flatMap(Class003_map::getCharacterStream);  //[a,a,a,b,b,b,c,c,c]

        //3.终止行为
        stream1.forEach(System.out::println);

        //流中操作流
        /*stream2.forEach(s->{
            s.forEach(System.out::println);
        });*/

        stream2.forEach(System.out::println);
    }

    //测试 : 接收一个字符串,返回每一个字符串中所有字符的流
    public static Stream<Character> getCharacterStream(String str){
        //1.转为字符数组
        List<Character> ls = new ArrayList<>();
        for(char ch : str.toCharArray()){
            ls.add(ch);
        }
        return ls.stream();
    }
}

3.终止行为各种方法(返回的结果不在是stream)

/*
    终止行为:
        查找与匹配
        收集 collect()
 */
public class Class04_Stream {
    static List<Employee> emps = Arrays.asList(
            new Employee(102,"张三", 18, 3333.33),
            new Employee(101,"李四", 38, 4444.44),
            new Employee(104,"王五", 50, 5555.55),
            new Employee(104,"王五", 50, 5555.55),
            new Employee(103,"赵六", 16, 6666.66),
            new Employee(105,"田七", 28, 7777.77)
    );
    public static void main(String[] args) {
        //获取流
        Stream<Employee> stream = emps.stream();

        Stream<Employee> stream2 = stream.filter(employee -> employee.getAge() >= 18);
        //终止行为
        List<Employee> list = stream2.distinct()
                      .collect(Collectors.toList());
        list.forEach(employee -> System.out.println(employee));
        //list.forEach(System.out::println);

        System.out.println("--------------------分界线----------------------");
        System.out.println();
//        stream2.forEach(e-> System.out.println(e));

         需求:获取当前公司所有员工的姓名添加到集合中
        //toList()
        List<String> list2 = emps.stream()
                .map(Employee::getName)
                .collect(Collectors.toList());
        list.forEach(System.out::println);


        // 需求:找到员工工资最高的员工信息
        //1)按照工资做降序排序
        //找到第一个
        Optional<Employee> op = emps.stream().sorted((x,y)->Double.compare(x.getPrice(),y.getPrice())).findFirst();
//        op.isPresent() //判断是否为空值
        //findAny-返回当前流中的任意元素
        System.out.println(emps.stream().parallel().findAny().get());

        Double set = emps.stream()
                .collect(Collectors.summingDouble(Employee::getPrice));//summing 是累加方法
        System.out.println(set);

        //count-返回流中元素的总个数
        System.out.println(emps.stream().filter(employee -> employee.getAge() > 18).distinct().count());

        //2)max()
        System.out.println(emps.stream().max((x, y) -> Double.compare(x.getPrice(), y.getPrice())));

        //找到薪资最高的薪资值    Employee::getPrice
        System.out.println(emps.stream().map(Employee::getPrice).max(Double::compareTo).get());

        Set<Double> set2 = emps.stream().map(Employee::getPrice).collect(Collectors.toSet());
        set2.forEach(System.out::println);

        Map<Integer,String> may = emps.stream().collect(Collectors.toMap(Employee::getId,Employee::getName));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值