Java--Lambda

Lambda表达式的介绍

▪ Lambda表达式是 Java8 中最重要的新功能之一。使用 Lambda 表达 式可以替代只有一个抽象函数的接口实现,告别匿名内部类,代码看 起来更简洁易懂。Lambda表达式同时还提升了对集合、框架的迭代、 遍历、过滤数据的操作

Lambda表达式的特点 1:函数式编程 2:参数类型自动推断 3:代码量少,简洁

public class LambdaTest {
    public static void main(String[] args) throws Exception {
 
        /*
        在JDK中,提供了一些代表输入和输出的接口,他们本身是没有什么意义的,只是为了
            方便的使用lambda表达式来编写代码,从而获取对应的执行结果。
         */
 
        LambdaInterface li4 = (num)->{return num;};
        LambdaInterface li5 = (num)->num;
        LambdaInterface li6 = num->num;
        LambdaInterface li7 = num->{return num;};
 
        System.out.println(li4.get(300));
 
        Function<Integer,Integer> function = (num)->num;
        System.out.println(function.apply(100));
        Consumer<Integer> consumer1 = (num)-> System.out.println(num);
        consumer1.accept(1000);
        Consumer<Integer> consumer2 = (num)->getNum(num);
        consumer2.accept(2000);
 
        Function<String,String> f1 = (str)->str.toUpperCase();
        Function<String,String> f2 = (str)->toUpperCase(str);
        System.out.println(f1.apply("abcdefg"));
        System.out.println(f2.apply("abcdefgefg"));
 
        BiFunction<String,String,Integer> bf = (a,b)->a.length()+b.length();
        System.out.println(bf.apply("老于", "很帅"));
        BiFunction<String,String,Integer> bf1 = (a,b)->getStrLength(a,b);
        System.out.println(bf1.apply("老于", "很帅"));
 
 
    }
 
    public static void getNum(int num){
        System.out.println(num);
    }
 
    public static String toUpperCase(String str){
        return str.toUpperCase();
    }
 
    public static Integer getStrLength(String str1,String str2){
        return str1.length()+str2.length();
    }
 
//    public static int get(){
//        return 1;
//    }
//
//    public static String find(){
//        return "find";
//    }
//
//    public static void exec() {
//            find();
//    }
}
@FunctionalInterface
public interface StudentDao {
    public void insert(Student student);
}
public class Student {
}
public class Teacher {
}
@FunctionalInterface
public interface TeacherDao {
    public int get (Teacher teacher);
}

Lambda表达式应用场景 任何有函数式接口的地方

函数式接口 只有一个抽象方法(Object类中的方法除外)的接口是函数式接口

函数式接口

Supplier 代表一个输出

Consumer 代表一个输入

BiConsumer 代表两个输入

Function 代表一个输入,一个输出(一般输入和输出是不同类型的)

UnaryOperator 代表一个输入,一个输出(输入和输出是相同类型的)

BiFunction 代表两个输入,一个输出(一般输入和输出是不同类型的)

BinaryOperator 代表两个输入,一个输出(输入和输出是相同类型的)

 方法的引用

▪ 方法引用是用来直接访问类或者实例的已经存在的方法或者构造 方法,方法引用提供了一种引用而不执行方法的方式,如果抽象 方法的实现恰好可以使用调用另外一个方法来实现,就有可能可 以使用方法引用

 方法的引用

▪ 静态方法引用:如果函数式接口的实现恰好可以通过调用一个静 态方法来实现,那么就可以使用静态方法引用

▪ 实例方法引用:如果函数式接口的实现恰好可以通过调用一个实 例的实例方法来实现,那么就可以使用实例方法引用

▪ 对象方法引用:抽象方法的第一个参数类型刚好是实例方法的类 型,抽象方法剩余的参数恰好可以当做实例方法的参数。如果函 数式接口的实现能由上面说的实例方法调用来实现的话,那么就 可以使用对象方法引用

▪ 构造方法引用:如果函数式接口的实现恰好可以通过调用一个类 的构造方法来实现,那么就可以使用构造方法引用

public class Student {
    private String name;
    private int age;
    private int score;
 
    public Student() {
    }
 
    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public int getScore() {
        return score;
    }
 
    public void setScore(int score) {
        this.score = score;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
public class Test {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("zhangfei",18,69));
        list.add(new Student("guanyu",20,70));
        list.add(new Student("zhaoyun",26,75));
        list.add(new Student("liubei",30,85));
        list.add(new Student("huangzhong",23,90));
 
        //查询年龄大于25岁的学生信息
        getByAge(list);
        //查询分数大于80分的学生信息
        System.out.println("***********************************");
        getByScore(list);
    }
    public static void getByAge(ArrayList<Student> students){
        ArrayList<Student> list = new ArrayList<>();
        for (Student student : students){
            if(student.getAge()>25){
                list.add(student);
            }
        }
        for(Student student : list){
            System.out.println(student);
        }
    }
 
    public static void getByScore(ArrayList<Student> students){
        ArrayList<Student> list = new ArrayList<>();
        for(Student student : students){
            if(student.getScore()>80){
                list.add(student);
            }
        }
        for(Student student : list){
            System.out.println(student);
        }
    }
}
public class Test {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("zhangfei",18,69));
        list.add(new Student("guanyu",20,70));
        list.add(new Student("zhaoyun",26,75));
        list.add(new Student("liubei",30,85));
        list.add(new Student("huangzhong",23,90));
 
        //查询年龄大于25岁的学生信息
        getByFilter(list,(student) -> student.getAge() >25);
 
        //查询大于80分的学生信息
        System.out.println("*********************");
        getByFilter(list,(student)->student.getScore() > 80);
        //查询姓名长度大于6的学生信息
        System.out.println("******************************");
        getByFilter(list,(student -> student.getName().length() >6));
 
    }
 
    public static void getByFilter(ArrayList<Student> students, StudentFilter filter){
        ArrayList<Student> list = new ArrayList<>();
        for (Student student : students){
            if(filter.compare(student)){
                list.add(student);
            }
        }
        printStudent(list);
    }
 
    public static void printStudent(ArrayList<Student> students){
        for (Student student1 : students){
            System.out.println(student1);
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值