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 use java method reference

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

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this "java method citation how to use" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "java method citation how to use" article.

1. Description

A method reference can be seen as a deep expression of an Lambda expression. In other words, a method reference is an Lambda expression, an example of a function interface, pointing to a method by its name.

2. Use the scene

When the operation to be passed to the Lambda body, the method has been implemented, you can use the method reference

3. Format

Class (or object):: method name

4. Examples

Public class MethodRefTest {/ / case 1: object: void accept (T t) in / Consumer / void println (T t) @ Test public void test1 () {/ / use Lambda to express Consumer con1 = str-> System.out.println (str); con1.accept (China); System.out.println (=) / / use method reference PrintStream ps = System.out; Consumer con2 = ps::println; con2.accept ("China");} / / T get () in Supplier / / String getName () @ Test public void test2 () {/ / use Lambda to express Employee emp = new Employee (1001, "Bruce", 34600) Supplier sup1 = ()-> emp.getName (); System.out.println (sup1.get ()); System.out.println ("="); / / use method reference Supplier sup2 = emp::getName; System.out.println (sup2.get ()) } / / case 2: class: int compare in static method / / Comparator (T2) / / int compare in Integer (T2) @ Test public void test3 () {/ / use Lambda to express Comparator com1 = (T1, T2)-> Integer.compare (T1, T2); System.out.println (com1.compare (32,45)) System.out.println ("="); / / use method reference Comparator com2 = Integer::compareTo; System.out.println (com2.compare (43,34)) } / / Long round (Double d) @ Test public void test4 () in R apply (T t) / / Math in / / Function {/ / use anonymous inner class Function func = new Function () {@ Override public Long apply (Double aDouble) {return Math.round (aDouble);}} System.out.println (func.apply); System.out.println ("="); / / use the Lambda expression Function func1 = d-> Math.round (d); System.out.println (func1.apply (12.3)); System.out.println ("="); / / use method reference Function func2 = Math::round System.out.println (func2.apply);} / / case 3: class: int comapre in example method / / Comparator (T T1 and T2) / / String int t1.compareTo (T2) @ Test public void test5 () {/ / use the Lambda expression Comparator com1 = (S1, S2)-> s1.compareTo (S2) System.out.println (com1.compare ("abd", "aba")); System.out.println ("="); / / method reference Comparator com2 = String::compareTo; System.out.println (com2.compare ("abd", "abc"));} / / boolean test (T T1, T T2) in BiPredicate Boolean t1.equals (T2) @ Test public void test6 () in / / String {/ / use the Lambda expression BiPredicate pre1 = (S1, S2)-> s1.equals (S2); System.out.println (pre1.test ("abc", "abc")); System.out.println ("="); / / use method reference BiPredicate pre2 = String::equals System.out.println (pre2.test ("abc", "abd")); R apply (T t) in / / Function / String getName () in / Employee; @ Test public void test7 () {/ / use the Lambda expression Employee employee = new Employee (1001, "Tom", 45, 10000); Function func1 = e-> e.getName (); System.out.println (func1.apply (employee)) System.out.println ("="); / / use method reference Functionfunc2 = Employee::getName; System.out.println (func2.apply (employee)) }} the above is about the content of this article on "how to use java method quotation". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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