24 Java8新特性

lix_uan
• 阅读 1026

Lamabda表达式

函数式编程思想

  • 面向对象的思想

    • 做一件事情,找一个能解决这个事情的对象,调用对象的方法,完成事情
  • 函数式编程思想

    • 只要能获取结果,谁去做的,怎么做的都不重要,只重视结果,不重视过程

Lambda表达式语法

(形参列表) -> {Lambda体}

简化策略

  • 当{Lambda体}中只有一句语句时,可以省略{}和{;}
  • 当{Lambda体}中只有一句语句时,并且这个语句还是一个return语句,那么return也可以省略,但是如果{;}没有省略的话,return是不能省略的
  • (形参列表)的类型可以省略
  • 当(形参列表)的形参个数只有一个,那么可以把数据类型和()一起省略,但是形参名不能省略
  • 当(形参列表)是空参时,()不能省略

函数式接口

  • 接口中有且仅有一个抽象方法

    public class TestLambda {
        public static void main(String[] args) {
            callSomething(()->System.out.println("回家吃饭"));
            callSomething(()->System.out.println("我爱你"));
            callSomething(()->System.out.println("滚蛋"));
            callSomething(()->System.out.println("回来"));
        }
        public static void callSomething(Call call){
            call.shout();
        }
    }
    interface Call {
        void shout();
    }

StreamAPI

注意

  • Stream自己不会存储元素
  • Stream不会改变源对象,每次处理都会返回一个持有结果的新Stream

Stream流图示

24 Java8新特性

Stream流的创建

package com.atguigu.test06;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.Test;

public class Test07StreamCreate {
    @Test
    public void test06(){
        /*
         * Stream<T> iterate(T seed, UnaryOperator<T> f)  
         * UnaryOperator接口,SAM接口,抽象方法:
         * 
         * UnaryOperator<T> extends Function<T,T>
         *         T apply(T t)
         */
        Stream<Integer> stream = Stream.iterate(1, num -> num+=2);
//        stream = stream.limit(10);
        stream.forEach(System.out::println);
    }

    @Test
    public void test05(){
        Stream<Double> stream = Stream.generate(Math::random);
        stream.forEach(System.out::println);
    }

    @Test
    public void test04(){
        Stream<Integer> stream = Stream.of(1,2,3,4,5);
        stream.forEach(System.out::println);
    }

    @Test
    public void test03(){
        String[] arr = {"hello","world"};
        Stream<String> stream = Arrays.stream(arr);
    }

    @Test
    public void test02(){
        int[] arr = {1,2,3,4,5};
        IntStream stream = Arrays.stream(arr);
    }

    @Test
    public void test01(){
        List<Integer> list = Arrays.asList(1,2,3,4,5);

        //JDK1.8中,Collection系列集合增加了方法
        Stream<Integer> stream = list.stream();
    }
}

Stream流的中间操作

package com.atguigu.test06;

import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.Test;

public class Test08StreamMiddle {

    @Test
    public void test12(){
        String[] arr = {"hello","world","java"};
        Arrays.stream(arr)
            .flatMap(t -> Stream.of(t.split("|")))//Function<T,R>接口抽象方法 R apply(T t)  现在的R是一个Stream
            .forEach(System.out::println);
    }


    @Test
    public void test11(){
        String[] arr = {"hello","world","java"};

        Arrays.stream(arr)
            .map(t->t.toUpperCase())
            .forEach(System.out::println);
    }

    @Test
    public void test10(){
        Stream.of(1,2,3,4,5)
            .map(t -> t+=1)//Function<T,R>接口抽象方法 R apply(T t)
            .forEach(System.out::println);
    }

    @Test
    public void test09(){
        //希望能够找出前三个最大值,前三名最大的,不重复
        Stream.of(11,2,39,4,54,6,2,22,3,3,4,54,54)
            .distinct()
            .sorted((t1,t2) -> -Integer.compare(t1, t2))//Comparator接口  int compare(T t1, T t2)
            .limit(3)
            .forEach(System.out::println);
    }

    @Test
    public void test08(){
        long count = Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
            .distinct()
            .peek(System.out::println)  //Consumer接口的抽象方法  void accept(T t)
            .count();
        System.out.println("count="+count);
    }


    @Test
    public void test07(){
        Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
            .skip(5)
            .distinct()
            .filter(t -> t%3==0)
            .forEach(System.out::println);
    }

    @Test
    public void test06(){
        Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
            .skip(5)
            .forEach(System.out::println);
    }

    @Test
    public void test05(){
        Stream.of(1,2,2,3,3,4,4,5,2,3,4,5,6,7)
            .distinct()  //(1,2,3,4,5,6,7)
            .filter(t -> t%2!=0) //(1,3,5,7)
            .limit(3)
            .forEach(System.out::println);
    }


    @Test
    public void test04(){
        Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
            .limit(3)
            .forEach(System.out::println);
    }


    @Test
    public void test03(){
        Stream.of(1,2,3,4,5,6,2,2,3,3,4,4,5)
            .distinct()
            .forEach(System.out::println);
    }


    @Test
    public void test02(){
        Stream.of(1,2,3,4,5,6)
            .filter(t -> t%2==0)
            .forEach(System.out::println);
    }

    @Test
    public void test01(){
        //1、创建Stream
        Stream<Integer> stream = Stream.of(1,2,3,4,5,6);

        //2、加工处理
        //过滤:filter(Predicate p)
        //把里面的偶数拿出来
        /*
         * filter(Predicate p)
         * Predicate是函数式接口,抽象方法:boolean test(T t)
         */
        stream = stream.filter(t -> t%2==0);

        //3、终结操作:例如:遍历
        stream.forEach(System.out::println);
    }
}

Stream流的终结操作

package com.atguigu.test06;

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

import org.junit.Test;

public class Test09StreamEnding {

    @Test
    public void test14(){
        List<Integer> list = Stream.of(1,2,4,5,7,8)
                .filter(t -> t%2==0)
                .collect(Collectors.toList());

        System.out.println(list);
    }


    @Test
    public void test13(){
        Optional<Integer> max = Stream.of(1,2,4,5,7,8)
             .reduce((t1,t2) -> t1>t2?t1:t2);//BinaryOperator接口   T apply(T t1, T t2)
        System.out.println(max);
    }

    @Test
    public void test12(){
        Integer reduce = Stream.of(1,2,4,5,7,8)
             .reduce(0, (t1,t2) -> t1+t2);//BinaryOperator接口   T apply(T t1, T t2)
        System.out.println(reduce);
    }

    @Test
    public void test11(){
        Optional<Integer> max = Stream.of(1,2,4,5,7,8)
                .max((t1,t2) -> Integer.compare(t1, t2));
        System.out.println(max);
    }

    @Test
    public void test10(){
        Optional<Integer> opt = Stream.of(1,2,4,5,7,8)
                .filter(t -> t%3==0)
                .findFirst();
        System.out.println(opt);
    }

    @Test
    public void test09(){
        Optional<Integer> opt = Stream.of(1,2,3,4,5,7,9)
                .filter(t -> t%3==0)
                .findFirst();
        System.out.println(opt);
    }

    @Test
    public void test08(){
        Optional<Integer> opt = Stream.of(1,3,5,7,9).findFirst();
        System.out.println(opt);
    }

    @Test
    public void test04(){
        boolean result = Stream.of(1,3,5,7,9)
            .anyMatch(t -> t%2==0);
        System.out.println(result);
    }


    @Test
    public void test03(){
        boolean result = Stream.of(1,3,5,7,9)
            .allMatch(t -> t%2!=0);
        System.out.println(result);
    }

    @Test
    public void test02(){
        long count = Stream.of(1,2,3,4,5)
                .count();
        System.out.println("count = " + count);
    }

    @Test
    public void test01(){
        Stream.of(1,2,3,4,5)
                .forEach(System.out::println);
    }
}

Stream流练习

public static void main(String[] args) {
       //第一支队伍
        ArrayList<String> one = new ArrayList<>();
        one.add("迪丽热巴");
        one.add("宋远桥");
        one.add("苏星河");
        one.add("石破天");
        one.add("石中玉");
        one.add("老子");
        one.add("庄子");
        one.add("洪七公");

        //第二支队伍
        ArrayList<String> two = new ArrayList<>();
        two.add("古力娜扎");
        two.add("张无忌");
        two.add("赵丽颖");
        two.add("张三丰");
        two.add("尼古拉斯赵四");
        two.add("张天爱");
        two.add("张二狗");

        // 第一个队伍只要名字为3个字的成员姓名;
        // 第一个队伍筛选之后只要前3个人;
        Stream<String> streamOne = one.stream().filter(s ‐> s.length() == 3).limit(3);

        // 第二个队伍只要姓张的成员姓名;
        // 第二个队伍筛选之后不要前2个人;
        Stream<String> streamTwo = two.stream().filter(s ‐> s.startsWith("张")).skip(2);

        // 将两个队伍合并为一个队伍;
        // 根据姓名创建Person对象;
        // 打印整个队伍的Person对象信息。
        Stream.concat(streamOne, streamTwo).map(Person::new).forEach(System.out::println);      
}
点赞
收藏
评论区
推荐文章

暂无数据

lix_uan
lix_uan
Lv1
学无止境,即刻前行
文章
7
粉丝
5
获赞
0