JDK8特性入门学习1-Lambda表达式

1、表达式参数列表 -> 执行的功能

格式1:无参、无返回值

格式2:有1参、无返回值,可以不写小括号

格式3:有2个以上参、有返回值、lambda中有多条语句

格式4:有2个以上参、有返回值、lambda中只有一条语句,可以不写大括号和return

格式5:Lambda表达式参数列表数据类型可以省略不写,JVM可以通过上下文推断出,即“类型推断”

总结

上联:左右遇一括号省
下联:左侧推断类型省
横批:能省则省

2、Lambda表达式需要“函数式接口”的支持

“函数式接口”定义

接口中只有一个抽象方法的接口

3、Java 8 内置的四大核心函数式接口

Consumer:消费型接口

    void accept(T t);
    
    // case:消费
    @Test
    public void test2() {
        consumerMoney(1000d, (m) -> System.out.println("消费" + m + "元"));
    }
    
    private static void consumerMoney(Double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

Supplier:供给型接口

    T get();
    
    // case:生成指定个数的整数放入集合中
    @Test
    public void test3() {
        List<Integer> numList = getNumList(10, () ->(Math.random() * 100));
        for (Integer num : numList) {
            System.out.println(num);
        }
    }
    
    private static List<Integer> getNumList(int num, Supplier<Double> supplier) {
        List<Integer> numList = Lists.newArrayList();
        for (int i = 0; i < num; i++) {
            numList.add(supplier.get().intValue());
        }
        return numList;
    }

Function<T,R>:函数型接口

    R apply(T t);

	// case:处理字符串    
    @Test
    public void test4() {
        String s = filterStr("\b\b 函数型接口  ", str -> str.trim());
        System.out.println(s);
    }
    
    private static String filterStr(String oldStr, Function<String, String> function) {
        return function.apply(oldStr);
    }

Predicate:断言型接口

    boolean test(T t);
   
    // case:将满足条件的字符串放入集合中 
    @Test
    public void test5() {
        List<String> oldStrList = Arrays.asList("aaa", "bbbb", "cc", "dd", "e");
        List<String> newStrList = putStrToList(oldStrList, x -> x.length() < 3);
        for (String s : newStrList) {
            System.out.println(s);
        }
    }
    
    private static List<String> putStrToList(List<String> oldStrList, Predicate<String> predicate) {
        List<String> newStrList = Lists.newArrayList();
        for (String str : oldStrList) {
            if (predicate.test(str)) {
                newStrList.add(str);
            }
        }
        return newStrList;
    }

4、方法引用

定义:Lambda体中的内容有方法已经实现

格式1:对象::实例方法名

格式2:类::静态方法名

格式3:类::实例方法名

注意点:Lambda体中调用方法的参数列表与返回值类型要与函数式接口中抽象方法的函数列表和返回值类型保持一致

5、构造器引用

格式:ClassName::new

注意点:需要调用的构造器的参数列表要与函数式接口中抽象方法的参数列表保持一致

6、数组引用

格式:Type::new

    @Test
    public void test6() {
        // 对象::实例方法名
        Consumer consumer = (x) -> System.out.println(x);
        Consumer consumer1 = System.out::println;
        consumer1.accept("bbb");
    
        // 类::静态方法名
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        Comparator<Integer> com1 = Integer::compare;
        int result = com1.compare(1, 2);
        System.out.println(result);
    
        // 类::实例方法名
        BiPredicate<String, String> biPredicate = (x, y) -> x.equals(y);
        BiPredicate<String, String> biPredicate1 = String::equals;
        boolean test = biPredicate1.test("b", "b");
        System.out.println(test);
    
        // 无参构造器引用
        Supplier<Item> supplier = () -> new Item();
        Supplier<Item> supplier1 = Item::new;
        System.out.println(JSON.toJSONString(supplier1));
    
        // 一个参数构造器引用
        Function<Long, Item> supplier2 = Item::new;
    
        // 数组引用
        Function<Integer, String[]> fun = (x) -> new String[x];
        Function<Integer, String[]> fun1 = String[]::new;
        String[] strings = fun.apply(10);
        System.out.println(strings.length);
    }

Item对象

    public class Item {
        /**
         * 商品编号
         */
        public Long numId;
        /**
         * 商品名称
         */
        public String title;
        /**
         * 商品销量
         */
        public Long volume;
        /**
         * 商品的库存
         */
        public Long num;
        /**
         * 商品价格
         */
        public Double price;
    
        public Item() {
        }
    
        public Item(Long numId) {
            this.numId = numId;
        }
    
        public Item(Long numId, String title, Long volume, Long num, Double price) {
            this.numId = numId;
            this.title = title;
            this.volume = volume;
            this.num = num;
            this.price = price;
        }
    
        public Long getNumId() {
            return numId;
        }
    
        public void setNumId(Long numId) {
            this.numId = numId;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public Long getVolume() {
            return volume;
        }
    
        public void setVolume(Long volume) {
            this.volume = volume;
        }
    
        public Long getNum() {
            return num;
        }
    
        public void setNum(Long num) {
            this.num = num;
        }
    
        public Double getPrice() {
            return price;
        }
    
        public void setPrice(Double price) {
            this.price = price;
        }
    
        @Override
        public String toString() {
            return "Item{" +
                    "numId=" + numId +
                    ", title='" + title + '\'' +
                    ", volume=" + volume +
                    ", num=" + num +
                    ", price=" + price +
                    '}';
        }
    }

猜你喜欢

转载自blog.csdn.net/wy798393546/article/details/95306525