您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java8的lamdba表達式怎么實現對抽象接口”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java8的lamdba表達式怎么實現對抽象接口”吧!
總結:lamdba表達式 精髓就是對抽象接口方法的一個實現
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型數據處理 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表達式怎么實現對抽象接口”的內容了,經過本文的學習后,相信大家對Java8的lamdba表達式怎么實現對抽象接口這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。