您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。