In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what methods are quoted in Java". In daily operation, I believe many people have doubts about what methods are quoted in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what methods are quoted in Java?" Next, please follow the editor to study!
One: simplicity
There are three kinds of method references. Method references are represented by a pair of double colons::. Method references are another way of writing a functional interface.
Static method reference, through class name:: static method name, such as Integer::parseInt
Instance method reference, through instance object:: instance method, such as str::substring
Constructor reference, through the class name:: new, such as User::new
Two: method reference
Public final class Integer {
Public static int parseInt (String s) throws NumberFormatException {
Return parseInt (SJ10)
}
}
Through the method reference, you can assign the reference of the method to a variable, and by assigning a value to Function, it shows that the method reference is also a kind of functional interface writing, Lambda expression is also a kind of functional interface, Lambda expression is generally used to provide its own method body, and method reference generally directly references off-the-shelf methods.
Public class User {
Private String username
Private Integer age
Public User () {
}
Public User (String username, Integer age) {
This.username = username
This.age = age
}
@ Override
Public String toString () {
Return "User {" +
"username='" + username +'\'+
", age=" + age +
'}'
}
/ / Getter&Setter
}
Public static void main (String [] args) {
/ / use double colons:: to construct static function references
Function fun = Integer::parseInt
Integer value = fun.apply ("123")
System.out.println (value)
/ / use double colons:: to construct non-static function references
String content = "Hello JDK8"
Function func = content::substring
String result = func.apply (1)
System.out.println (result)
/ / Constructor reference
BiFunction biFunction = User::new
User user = biFunction.apply ("mengday", 28)
System.out.println (user.toString ())
/ / function reference is also a functional interface, so you can also use function reference as a parameter to a method.
SayHello (String::toUpperCase, "hello")
}
/ / the method has two parameters, one is
Private static void sayHello (Function func, String parameter) {
String result = func.apply (parameter)
System.out.println (result)
}
Three: optional values for Optional
There is Optional in Google Guava and a similar syntax in Swift. In Swift, optional values are treated as a data type, and the status is equal to the basic type, with a very high status.
Package java.util
Import java.util.function.Consumer
Import java.util.function.Function
Import java.util.function.Predicate
Import java.util.function.Supplier
/ * *
* @ since 1.8
, /
Public final class Optional {
Private static final Optional EMPTY = new Optional ()
Private final T value
Private Optional () {
This.value = null
}
/ / return an empty Optional instance
Public static Optional empty () {
@ SuppressWarnings ("unchecked")
Optional t = (Optional) EMPTY
Return t
}
Private Optional (T value) {
This.value = Objects.requireNonNull (value)
}
/ / returns the Optional with the current non-null value of Optional
Public static Optional of (T value) {
Return new Optional (value)
}
/ / returns an Optional with a specified value of Optional, or an empty Optional if it is not empty
Public static Optional ofNullable (T value) {
Return value = = null? Empty (): of (value)
}
/ / if there is a value in Optional, the value is returned, otherwise NoSuchElementException is thrown.
Public T get () {
If (value = = null) {
Throw new NoSuchElementException ("No value present")
}
Return value
}
/ / returns true if there is a value, otherwise false
Public boolean isPresent () {
Return value! = null
}
/ / if a value exists, the specified consumer is called with that value, otherwise no action is performed.
Public void ifPresent (Consumer
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.