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

Java 8 method reference and constructor reference, array reference example analysis

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "Java 8 method reference and constructor reference, array reference example analysis", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Java 8 method reference and constructor reference, array reference example analysis" bar!

Import org.junit.Test;import java.io.PrintStream;import java.util.Comparator;import java.util.function.BiFunction;import java.util.function.BiPredicate;import java.util.function.Consumer;import java.util.function.Function;import java.util.function.Supplier / * * 1. Method reference: if you already have a method to implement the function in the Lambda body, you can use method reference * (you can understand method reference as another form of Lambda expression) * * 1. Object reference:: instance method name * * 2. Class name: static method name * * 3. Class name: instance method name * * Note: * the parameter list and return value type of the method referenced by the ① method need to be consistent with the parameter list and return value type of the abstract method in the functional interface! * ② if the first parameter of the parameter list of Lambda is the caller of the instance method, and the second parameter (or no parameter) is the parameter of the instance method, the format: ClassName::MethodName * * 2.Constructor reference: constructor parameter list, which needs to be consistent with the parameter list in the functional API! * * 1. Class name:: new * * III. Array reference * * Type []: new; * * / public class TestMethodRef {/ / Array reference @ Test public void test8 () {Function fun = (args)-> new String [args]; String [] strs = fun.apply (10); System.out.println (strs.length) System.out.println ("- -"); Function fun2 = Employee []:: new; Employee [] emps = fun2.apply (20); System.out.println (emps.length);} / / Constructor reference @ Test public void test7 () {Function fun = Employee::new Employee em=fun.apply ("www"); System.out.println (em.getName ()); BiFunction fun2 = Employee::new;} @ Test public void test6 () {Supplier sup = ()-> new Employee (); System.out.println (sup.get ()) System.out.println ("-"); Supplier sup2 = Employee::new; System.out.println (sup2.get ()) } / / Class name: instance method name @ Test public void test5 () {BiPredicate bp = (x, y)-> x.equals (y); System.out.println (bp.test ("abcde", "abcde")); System.out.println ("- -") BiPredicate bp2 = String::equals; System.out.println (bp2.test ("abc", "abc")); System.out.println ("- -"); Function fun = (e)-> e.show () System.out.println (fun.apply (new Employee (); System.out.println ("- -"); Function fun2 = Employee::show; System.out.println (fun2.apply (new Employee () } / / Class name: static method name @ Test public void test4 () {Comparator com = (x, y)-> Integer.compare (x, y); System.out.println ("- -"); Comparator com2 = Integer::compare } @ Test public void test3 () {BiFunction fun = (x, y)-> Math.max (x, y); System.out.println (fun.apply (1.5,22.2)); System.out.println ("- -") BiFunction fun2 = Math::max; System.out.println (fun2.apply (1.2,1.5);} / object reference: instance method name @ Test public void test2 () {Employee emp = new Employee (101,1,1.5); Supplier sup = ()-> emp.getName (); System.out.println (sup.get ()) System.out.println ("- -"); Supplier sup2 = emp::getName; System.out.println (sup2.get ());} @ Test public void test1 () {PrintStream ps = System.out; Consumer con = (str)-> ps.println (str) Con.accept ("Hello World!") ; System.out.println ("- -"); Consumer con2 = ps::println; con2.accept ("Hello Java8!") ; Consumer con3 = System.out::println;}} Thank you for your reading. The above is the content of "Java 8 method reference and constructor reference, array reference example analysis". After the study of this article, I believe you have a deeper understanding of the problem of Java 8 method reference and constructor reference, array reference example analysis, 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