Lambda概念介绍

1.Lambda管中窥豹

lambda可以理解成一种匿名内部类的简写方式,行为参数化,他的结构是

参数     箭头   主体

如下图:

package com.three;

import java.util.Comparator;

public class LambdaIntroduce {

    public static void main(String[] args){

        Comparator<Integer> intCompare = new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        };

        //by using Lambda
        Comparator<Integer> intCompareWithLambda = (Integer a1, Integer a2) -> a1.compareTo(a2);

    }
}

 这里a1和a2就是参数,a1.compareTo(a2)就是主体

 Lambda语法有两种表达方式,我的理解就是隐式的return和显式的return.·

1.(parameters) -> expression

  如 (String s1,String s2) -> s1.equals(s2); 

    ()->"Hello"

2.(parameters) ->{statements;}

  如 (String s1,String s2) ->{return s1..equals(s2);}

    ()->{return "Hello";}          

 

2.函数式接口、函数描述符

函数式接口,就是指的有且仅有一个抽象方法的接口

如下:

package com.three;

@FunctionalInterface
public interface FunctionInterface {
    int compare(int t1, int t2);
}

通常会用注解@FunctionalInterface来表示这是一个函数式接口

其中定义的抽象方法就叫做函数描述符

int compare(int t1, int t2);

Lambda只能应用于函数式接口上

 

3.函数式接口的使用

3.1 Predicate<T>

函数描述符是boolean test(T t),表示接受一个泛型,返回boolean值,T->boolean

/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.util.function;

import java.util.Objects;

/**
 * Represents a predicate (boolean-valued function) of one argument.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #test(Object)}.
 *
 * @param <T> the type of the input to the predicate
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

LambdaUseOfPredicate.java:

package com.three;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class LambdaUseOfPredicate {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("ab", "ac", "bc", "bd", "");
        Predicate<String> noneEmptyPredicate = (String s) -> !s.isEmpty();
        filter(list, noneEmptyPredicate);

        System.out.println("-------------");

        Predicate<String> startWithAPredicate = (String s) -> s.startsWith("a");
        filter(list, startWithAPredicate);
    }

    /**
     * 过滤并保存满足predicate传递的行为的list中的数据集合
     *
     * @param list
     * @param predicate
     * @param <E>
     * @return
     */
    public static <E> List<E> filter(List<E> list, Predicate<E> predicate) {
        List<E> filterList = new ArrayList<>();
        for (E e : list) {
            if (predicate.test(e)) {
                filterList.add(e);
            }
        }
        for (E e : filterList){
            System.out.print(e);
            System.out.print("   ");
        }
        return filterList;
    }
}
  •   在函数式接口Predicate<T>上,通过使用lambda的语法定义了两个noneEmptyPredicate和startWithAPredicate,可以理解成给抽象方法test做了实现,也说明lambda是应用在函数式接口上的
  •   行为参数化:noneEmpty和startWithA是两种不同的行为,这里当成了filter函数的第二个参数了,这样的好处就是我把我要做的事情包装成了一个参数进入了filter方法,filter里面怎么过滤的,源代码是不用改的,这就是行为参数化,可以简化很多重复逻辑,不然filter需要多次重写

 

3.2 Consumer<T>

   函数描述符是void accept(T t),接受一个泛型对象,返回为空,即T->void,注意这里也是java.util.function下的函数式接口。个人感觉应用就是“纯消费”,只吃不吐,有入参,返回为空

package java.util.function;

import java.util.Objects;

/**
 * Represents an operation that accepts a single input argument and returns no
 * result. Unlike most other functional interfaces, {@code Consumer} is expected
 * to operate via side-effects.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #accept(Object)}.
 *
 * @param <T> the type of the input to the operation
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}
LambdaUseOfConsumer.java
package com.three;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class LambdaUseOfConsumer {


    public static void main(String[] args){
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        Consumer<Integer> printElementConsumer = (Integer a) -> System.out.println(a);
        forEach(list, printElementConsumer);

        Consumer<Integer> elementAddOneConsumer = (Integer a) -> {a++;};
        forEach(list, elementAddOneConsumer);
    }

    public static <E> void forEach(List<E> list, Consumer<E> consumer){
        for(E e : list){
            consumer.accept(e);
        }
    }
}

  这个例子用来加深理解行为参数化,这里消费者有两个行为,一个是打印每一个元素,一个是每个元素数值加1,这里把行为当成了函数forEach的参数,即行为参数化,这样子复用了方法forEach而不用根据行为不同而重构.

 

 3.3 Function<T, R>

 函数式接口是Function<T,R>,函数描述符是T->R,即一个泛型转换为另一个泛型

 

package java.util.function;

import java.util.Objects;

/**
 * Represents a function that accepts one argument and produces a result.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object)}.
 *
 * @param <T> the type of the input to the function
 * @param <R> the type of the result of the function
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值