Java学习笔记——Lambda表达式

Lambda表达式是java 8中包含的重要功能之一。Lambda表达式用于编写该接口的简洁代码,其中包含一个方法即功能界面。Lambda 表达式也用于以简单的方式迭代收集。对于单个方法接口,无论我们通过使用匿名类还是内部类实现,可以使用java 8中非常简洁的代码中的lambda表达式来实现.Lambda表达式定义了函数接口方法并返回该接口的实例。

Lambda表达式语法

Lambda表达式语法非常容易阅读和理解。Lambda表达式语法将看起来像

(Argument  part)  -> Body part 

Sample 1: 如果方法没有参数和打印消息。

() ->    System.out.println("Your message"); 

Sample 2 : 如果方法具有两个参数并执行一些业务逻辑并返回值。

(int a, int b) ->  a+b;

方法返回a + b的值。

Sample 3: 如果方法有一个参数并且做一些业务逻辑

  (String s) ->  s + "Hello World"; 

字符串将在连接后返回。

示例1:使用带有Lambda表达式的Runnable

在下面的例子中,我们运行一个Runnable线程。在lambda表达式之前,要实现可运行的线程,我们需要定义一个实现Runnable接口的类来获取可运行的对象。现在看看如何实现使用lambda表达式的同一个对象。
RunnableDemo.java

package com.concretepage.lambda;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class RunnableDemo {
    public static void main(String[] args) {
    final ExecutorService exService = Executors.newSingleThreadExecutor();
        Runnable r = () -> System.out.println("Lambda Expression Test with Runnable");
        exService.execute(r);
     }
}

输出结果:Lambda Expression Test with Runnable

示例2:用户定义的具有Lambda表达式的Functional Interface示例

Calculator.java

package com.concretepage.lambda;
public interface Calculator {
    public int add(int n1, int n2);    
}

CalculatorDemo

package com.concretepage.lambda;
public class CalculatorDemo {
    public static void main(String[] args) {
        Calculator cal =(int a, int b) -> a+b;
        int res = cal.add(5, 6);
        System.out.println(res);
    }
}

运行结果:11

示例3:使用比较器与Lambda表达式

现在看到Comparator接口如何使用lambda表达式对包含用户定义对象的集合进行排序。
ComparatorDemo.java

package com.concretepage.lambda;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorDemo {
    public static void main(String[] args) {
        List<Student> list = new ArrayList();
        list.add(new Student("Ram",20));
        list.add(new Student("Shyam",22));
        list.add(new Student("Kabir",18));
        System.out.println("...befor sorting...");
        for(Student s : list){
            System.out.println(s.getName());
        }    
        //define comparator
        Comparator<Student> comp= (Student s1, Student s2) -> s1.getName().compareTo(s2.getName());
        Collections.sort(list, comp);
        System.out.println("...after sorting...");
        for(Student s : list){
            System.out.println(s.getName());
        }
    }
}

Student.java

package com.concretepage.lambda;
public class Student {
    private String name;
    private int age;
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }
    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;
    }
 } 

运行结果:

...befor sorting...
Ram
Shyam
Kabir
...after sorting...
Kabir
Ram
Shyam
Example 4: Using Function Interface to Iterate Collection with Lambda Expression

In java 8 there is java.util.function package introduced. With the help of java.util.function.Function API, we will iterate collection using lambda expression. In Function interface there is apply() method that will be called in user defined function. Suppose we have to create a method for custom print then we will define the method as below.

public  String customShow(Function<Student,String> fun){
    return fun.apply(this);
}
package com.concretepage.lambda;
import java.util.ArrayList;
import java.util.List;
public class FunctionDemo {
    public static void main(String[] args) {
        List<Student> list = new ArrayList();
        list.add(new Student("Ram",20));
        list.add(new Student("Shyam",22));
        list.add(new Student("Kabir",18));
        for(Student st: list){
            System.out.println(st.customShow(s->s.getName()+": "+s.getAge()));
        }
    }
} 

运行结果:

Ram: 20
Shyam: 22
Kabir: 18
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值