溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java8的lamdba表達(dá)式怎么實現(xiàn)對抽象接口

發(fā)布時間:2021-11-24 16:43:19 來源:億速云 閱讀:143 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“Java8的lamdba表達(dá)式怎么實現(xiàn)對抽象接口”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java8的lamdba表達(dá)式怎么實現(xiàn)對抽象接口”吧!

 總結(jié):lamdba表達(dá)式 精髓就是對抽象接口方法的一個實現(xiàn)

import org.junit.jupiter.api.Test;import java.util.Arrays;import java.util.Collections;import java.util.List;public class TestLambda {

    List<Employee> emps = Arrays.asList(new Employee(101, "張三", 18, 9999.99),            new Employee(102, "李四", 59, 6666.66),            new Employee(103, "王五", 28, 3333.33),            new Employee(104, "趙六", 8, 7777.77),            new Employee(105, "田七", 38, 5555.55)
    );    @Test    public void test1(){
        Collections.sort(emps, (x,y)->{if(x.getAge()==y.getAge()){return x.getName().compareTo(y.getName());            }else {//                return -Integer.compare(x.getAge(), y.getAge());                return x.getAge()-y.getAge();            }

        });        for (Employee emp : emps) {
            System.out.println(emp);        }
    }@Test    public void test2(){
        String str=strHandler("  哎,你是軟硬都不吃  ",(x)->x.trim());        System.out.println(str);        String str1=strHandler("哎,你是軟硬都不吃", (x)->x.substring(4, 6));        System.out.println(str1);    }// 用于處理字符串    public String strHandler(String str,MyFunction mf){return mf.getValue(str);    }@Test    public void test3(){
        op(100L, 200L, (x,y)->x*y);    }// 用于處理兩個long型數(shù)據(jù)處理    public void op(Long l1,Long l2,MyFunction2<Long,Long> my){
        System.out.println(my.getValue(l1, l2));    }
}

MyFunction2類:

public interface MyFunction2<T, R> {   public R getValue(T t1, T t2);   }

MyFunction 類:

@FunctionalInterfacepublic interface MyFunction {   
   public String getValue(String str);}

Employee 類:

public class Employee {   private int id;   private String name;   private int age;   private double salary;   public Employee() {
   }   public Employee(String name) {      this.name = name;   }   public Employee(String name, int age) {      this.name = name;      this.age = age;   }   public Employee(int id, String name, int age, double salary) {      this.id = id;      this.name = name;      this.age = age;      this.salary = salary;   }   public int getId() {      return id;   }   public void setId(int id) {      this.id = id;   }   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 double getSalary() {      return salary;   }   public void setSalary(double salary) {      this.salary = salary;   }   public String show() {      return "測試方法引用!";   }   @Override   public int hashCode() {      final int prime = 31;      int result = 1;      result = prime * result + age;      result = prime * result + id;      result = prime * result + ((name == null) ? 0 : name.hashCode());      long temp;      temp = Double.doubleToLongBits(salary);      result = prime * result + (int) (temp ^ (temp >>> 32));      return result;   }   @Override   public boolean equals(Object obj) {      if (this == obj)         return true;      if (obj == null)         return false;      if (getClass() != obj.getClass())         return false;      Employee other = (Employee) obj;      if (age != other.age)         return false;      if (id != other.id)         return false;      if (name == null) {         if (other.name != null)return false;      } else if (!name.equals(other.name))         return false;      if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))         return false;      return true;   }   @Override   public String toString() {      return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";   }

}

感謝各位的閱讀,以上就是“Java8的lamdba表達(dá)式怎么實現(xiàn)對抽象接口”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Java8的lamdba表達(dá)式怎么實現(xiàn)對抽象接口這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI