In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "Java8 method reference and construction reference overview and example code", 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 "method reference and construction reference overview and example code in Java8"!
I. Overview of method references
A method reference is an abbreviation for a specific Lamda expression. The idea is to call the function directly using the method name if you can replace the Lamda expression. Its syntax format: class name:: method name.
Two and three methods are quoted.
1 A reference to a static method
Syntax format: static class name (ClassName):: method name (MethodName)
Example:
/ / 1 Lamda static method @ Test public void LamdaSTest () {String youku1327 = "1327"; Function function = s-> ObjectUtils.allNotNull (youku1327); System.out.println (function.apply (youku1327)); / / true} / / static method reference @ Test public void MethodRefTest () {String youku1327 = "youku1327"; Function function = ObjectUtils::allNotNull; System.out.println (function.apply (youku1327)); / / true}
2 references to methods that point to the object instance
Syntax format: instance name (instanceName):: method name (MethodName)
This object refers to an external object that is not an input parameter
Example:
/ / 2 Lamda expression @ Test public void ObjectLamdaTest () {Car car = new Car, "black", "China", 20); Supplier supplier = ()-> car.getColor (); System.out.println (supplier.get ()); / / black} / / object reference @ Test public void ObjectRefTest () {Car car = new Car ("100s", "black", "China", 20); Supplier supplier = car::getColor; System.out.println (supplier.get ()) / / black}
3 method reference pointing to the instance
Syntax format: object name (ClassName):: method name (Method) this object refers to the input parameter object
/ / 3 Lamda expression @ Test public void InstanceMethodLamdaTest () {Car car = new Car ("100s", "black", "China", 20); Function function = s-> s.getColor (); System.out.println (function.apply (car)); / / black} @ Test public void InstanceMethodRefTest () {Car car = new Car ("100s", "black", "China", 20); Function function = Car::getColor; System.out.println (function.apply (car)); / / black}
III. Constructor reference
Syntax format: object name (ClassName):: new
@ Test public void constructLamdaTest () {BiFunction biFunction = (youku1327 aDouble)-> new Car; Car car = biFunction.apply ("youku1327", 50.0); / / Car (code=youku1327, color=null, factory=null, price=50.0) System.out.println (car);} @ Test public void construcMethodRefTest () {BiFunction biFunction = Car::new; Car car = biFunction.apply ("youku1327", 50.0); / / Car (code=youku1327, color=null, factory=null, price=50.0) System.out.println (car);}
Thank you for your reading, the above is the "Java8 method reference and construction reference overview and example code" content, after the study of this article, I believe you on the Java8 method reference and construction reference overview and example code of this problem has a deeper understanding, the specific use of the need for you to practice verification. 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.