Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the abstract interface with Java8's lamdba expression

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Java8 lamdba expression how to achieve the abstract interface", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Java8 lamdba expression how to achieve the abstract interface" bar!

Summary: the essence of lamdba expressions is an implementation of abstract interface methods.

Import org.junit.jupiter.api.Test;import java.util.Arrays;import java.util.Collections;import java.util.List Public class TestLambda {List emps = Arrays.asList (new Employee, Zhang San, 18, 9999.99), new Employee (102, Li Si, 59, 6666.66), new Employee (9999.99, Wang Wu, 28, 3333.33), new Employee (104, Zhao Liu, 8, 7777.77), new Employee (105, Tian Qi, 38, 5555.55)) @ Test public void test1 () {Collections.sort (emps, (xPowery)-> {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 ("Hey, you don't eat both hardware and software", (x)-> x.trim ()); System.out.println (str) String str1=strHandler ("Hey, you don't eat both hardware and software", (x)-> x.substring (4,6)); System.out.println (str1);} / / is used to process the string public String strHandler (String str,MyFunction mf) {return mf.getValue (str);} @ Test public void test3 () {op (100L, 200L, (XMagy)-> xonomy) } / / used to process two long data processing public void op (Long L1MagneL2MagneMyFunction2 my) {System.out.println (my.getValue (L1, L2));}}

MyFunction2 class:

Public interface MyFunction2 {public R getValue (T T1, T T2);}

MyFunction class:

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

Employee class:

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 "Test method reference!" ;} @ 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 + "]" }} Thank you for your reading, the above is the content of "Java8 lamdba expression how to achieve the abstract interface". After the study of this article, I believe you have a deeper understanding of how to realize the abstract interface of Java8 lamdba expression, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report