Java8的lamdba表达式怎么实现对抽象接口
这篇文章主要讲解了“Java8的lamdba表达式怎么实现对抽象接口”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java8的lamdba表达式怎么实现对抽象接口”吧!
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、网站设计、海口网络推广、小程序定制开发、海口网络营销、海口企业策划、海口品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供海口建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
总结:lamdba表达式 精髓就是对抽象接口方法的一个实现
import org.junit.jupiter.api.Test;import java.util.Arrays;import java.util.Collections;import java.util.List;public class TestLambda { Listemps = 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 my){ System.out.println(my.getValue(l1, l2)); } }
MyFunction2类:
public interface MyFunction2{ 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表达式怎么实现对抽象接口这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
标题名称:Java8的lamdba表达式怎么实现对抽象接口
网站路径:http://myzitong.com/article/giosoj.html