JAVA8新特性Stream学习中

Stream是数据渠道,用于操作数据源(集合,数组等)所生成的元素序列。

"集合讲的是数据,流讲的是计算"

注:①Stream自己不会存储元素

        ②Stream不会改变源对象。相反,他们会返回一个持有结果的新的Stream

       ③Stream操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。

步骤:

①:创建Stream(数据源:如集合,数组,获得一个流)

②:中间操作(一个中间操作链,对数据源的数据进行处理)

③:终止操作,终端操作(一个 终止操作,执行中间操作链,并产生结果)

一:创建Stream的四种方式

public class TestStream{
    @Test
    public void test(){
        // 创建Stream 4中方式

        //①Collection系列集合提供的Stream()或者是parallelStream()
        List<String> list = new ArrayList<>();
        Stream<String> stream1 = list.stream();
        
        //② 通过Arrays中的静态方法Stream()获取数组流
        Employee[] emps = new Employee[10];
        Stream<Employee> stream2 = Arrays.stream(emps);
        
        //③  通过Stream类中的静态方法
        Stream<String> stream3 = Stream.of("aa","bb","cc");

        //④创建无限流
            // 迭代
            Stream<Integer> stream4 = Stream.iterate(0, (x) -> x+2);
            // 生成
            Stream.generate(() -> Math.random());
        
        
    }
}

二:中间操作

多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为""惰性求职。

public class StreamTest2 {
        List<Employee> employees = Arrays.asList(
            new Employee("呜呜",18,3234),
            new Employee("发多少",13,32324)
        );

         /** ① 筛选和切片
            filter -- 接收Lambda,从流中排除某些元素
            limit  -- 截断流,使其元素不超过给定的数量
            skip(n) -- 跳过元素,返回一个扔掉了前n个元素的流,若流中元素不足n个,则返回一个空流。
                        与limit(n)互补
            distinct -- 筛选, 通过流所生成元素的hashCode()和equals()去除重复元素。
        **/
    @Test
   //内部迭代(Stream API 内部自己迭代)
    public void test(){
       //创建流
        //中间操作(如果没有终止操作的话,中间操作不会执行)
        Stream<Employee> stream1 = employees.stream()
                    .filter((e) -> {
            //中间操作
            return e.getAge() >35 ;
        });

            //终止操作:一次性执行全部内容,即"惰性求值"
            stream1 .forEach(System.out::println);
    
    }

    //外部迭代
    public void test2(){
        Iterator<Employee> it = employees.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }


    @Test
    public void testLimit(){
        employees.stream()
                //中间操作  具有短路功效 当查询出2条符合条件的数据后,立即终止操作,不会继续执            
                  行了。效率高
                   .filter((e) -> {
                    return e.getSalary() > 2000;
                })
                    .limit(2)
                 //终止操作
                    .forEach(System.out::println); 
    }


    //跳过前2个取
    @Test
    public void testSkip(){
        employees.stream()
                .filter( (e) -> e.getSalary() > 2000 )
                .skip(2)
                .forEach(System.out::println);
    }
    
       //去重操作
    @Test
    public void testDistinct(){
        employees.stream()
                .filter( (e) -> e.getSalary() > 2000 )
                .skip(2)
                .distinct()
                .forEach(System.out::println);
    }

    //映射操作 (map  -- 接收Lambda,将元素转化成其他形式或提取信息,接收一个函数作为参数, 
                           该函数会被应用到每个元素上,并将其映射成一个新的元素。
               flatMap -- 接收一个函数作为参数,将流中的每一个值都换成另一个流,然后把所有 
                               流连接成一个流)
    @Test
    public void test23(){
        List<String> list = Arrays.asList("aaa","bbb","ccc","ddd","eee");
        list.stream()
            .map((str) -> str.toUpperCase())
            .forEach(System.out::println);
        //------------------------------------------

        employees.stream()
                    .map(Employee::getName)
                    .forEach(System.out::println);
    }


}

猜你喜欢

转载自blog.csdn.net/qq_33146717/article/details/82182435