本篇文章為大家展示了Java中Lambda表達式用法是什么,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
lambda是一個匿名函數,我們可以把lambda表達式理解為是一段可以傳遞的代碼。
lambda簡明的地將代碼或方法作為參數傳遞進去執行。
“函數式編程”其核心是把函數作為值。
函數式接口 :只有一個 抽象方法的接口 稱之為 函數式接口。函數式接口可以使用@FunctionalInterface進行注解。
lambda表達式拆分為兩部分
左側:lambda 表達式的參數列表
右側:lambda 表達式中所需要執行的功能,即lambda體
@Test
public void test(){
// () -> System.out.println("Hello");
Runnable a = new Runnable(){
@Override
public void run(){
System.out.println("Hello")
}
};
//等同于
Runnable a1 = () -> System.out.println("Hello");
a1.run();
}@Test
public void test(){
//Consumer被注解@FunctionalInterface的接口(函數式接口) 唯一抽象方法 void accept(T t);
//左側參數 -> 右側執行體
Consumer<String> con = (x) -> System.out.println(x);
// x -> System.out.println(x);
con.accept("hahah");
}@Test
public void test(){
//Comparator被注解@FunctionalInterface的接口 舉例抽象方法 int compare(T o1,T o2);
Comparator<Integer> com = (x,y) -> {
System.out.println("hhaha0");
return (x < y) ? -1 : ((x == y) ? 0 : 1);
};
com.compare(1,2);
}注意:lambda表達式的參數類型可以省略不寫,因為jvm編譯器可以從上下文推斷出數據類型。即“類型推斷”如果要在參數里面寫數據類型,都要寫上。
class Employee {
private String name;
private int age;
private double salary;
//省略 get and set and constructor
}
interface MyPredicate<T> {
boolean test(T t);
}
public class Test{
static List<Employee> list = Arrays.asList(
new Employee("張三",10,1),
new Employee("里斯",20,1),
new Employee("王五",16,1),
new Employee("二三",30,1)
);
public static List<Employee> filterEmployee(List<Employee> list,MyPredicate<Employee> mp){
List<Employee> emps = new ArrayList<>();
for (Employee employee : list) {
if(mp.test(employee)){
emps.add(employee);
}
}
return emps;
}
@org.junit.Test
public void test1(){
//需要使用自定義的方法
List<Employee> list2 = filterEmployee(list,(e) -> e.getAge() >= 15);
list2.stream().map(Employee::getName).forEach(System.out::println);
}
@org.junit.Test
public void test2(){
//可以使用stream進行list集合的過濾 不使用自定義接口
List<Employee> list2 = list.stream().filter((e) -> e.getAge() >= 15).collect(Collectors.toList());
list2.stream().map(Employee::getName).forEach(System.out::println);
}
}創建一個MyFun接口使用@FunctionalInterface注解,并創建一個抽象方法Integer getValue(Integer num);在Test類對變量進行某種操作。
@FunctionalInterface
interface MyFun{
Integer getValue(Integer num);
}
public class Test{
@org.junit.Test
public void Test(){
operation(100,num -> ++num);
}
/**
* param1 num : 傳入的整形數
* param2 mf : 實現某種方式對 整形數 進行操作。
**/
public Integer operation(Integer num,MyFun mf){
return mf.getValue(num);
}
}class Employee {
private String name;
private int age;
private double salary;
@Override
public String toString() {
return "["+this.name+","+this.getAge()+","+this.getSalary()+"]";
}
//省略 getter and setter and constructor
}
public class Test {
List<Employee> list = Arrays.asList(
new com.bilibili.lambda.test1.Employee("張三",10,1),
new com.bilibili.lambda.test1.Employee("里斯",20,1),
new com.bilibili.lambda.test1.Employee("王五",16,1),
new Employee("二三",30,1)
);
@org.junit.Test
public void test(){
Collections.sort(list,(e1,e2) -> {
if(e1.getAge() == e2.getAge()){
return e1.getName().compareTo(e2.getName());
}else{
//比較年齡大小
return Integer.compare(e1.getAge(),e2.getAge());
}
});
for (Employee e: list) {
System.out.println(e);
}
}
}Consumer<T> : 消費性接口 void accept(T t);
Supplier<T> : 共給性接口 T get();
Function<T,R> : 函數性接口 T代表參數,R代表返回值 R apply(T t);
Predicate<T> :斷言性接口 boolean test(T t);
class Test{
@org.junit.Test
publilc void test(){
happy(10000,(money)->System.out.println("happy消費"+money+"元"));
}
public void happy(double money,Consumer<double> con){
con.accept(money);
}
}方法引用:若lambda體中的內同有方法已經實現了,我們可以使用“方法引用”
(可以理解為方法引用時lambda的另一種表現形式)
主要有三種語法格式:
對象::實例方法名
類::靜態方法名
類::實例方法名
class Test{
//對象::實例方法名
@org.junit.Test
public void test(){
Consumer<String> con = (x) -> System.out.println(x);
con.accept("haha");
Consumer<String> con2 = System.out::println;
con2.accept("haha");
}
//類::靜態方法名
@org.junit.Test
public void test2(){
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
Comparator<Integer> com2 = Integer::compare;
com.compare(1,2);
com2.compare(1,2);
}
//類::實例方法名
@org.junit.Test(){
BiPredicate<String,String> bp = (x,y) -> x.equals(y);
bp.test("a","a");
BiPredicate<String,String> bp2 = String::equals;
}
}格式:
CalssName::new
class Test{
@org.junit.Test
public void test(){
Supplier<String> sup = () -> new String();
//這里的構造器引用取決于 接口方法的參數 的個數。 此處函數式接口 T get(); 為無參抽象方法所以String在實例化時 也是實例化無參的構造方法 其他類也適用
Supplier<String> sup2 = String::new;
String str = sup2.get();
}
}上述內容就是Java中Lambda表達式用法是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。