- A: concise
- Two: Method reference
- Three: Optional
A: concise
There are three types of method references. A method reference is represented by a pair of double colons ::. A method reference is another way of writing a functional interface
- Static method reference, by class name :: Static method name, such as Integer::parseInt
- Instance method reference, by instance object :: Instance method, such as STR ::substring
- Constructor reference, by class name ::new, such as User::new
Two: Method reference
public final class Integer { public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }}Copy the code
Using a method reference, you can assign a method reference to a variable. By assigning a value to a Function, you can show that a method reference is also a way of writing a functional interface. A Lambda expression is usually used to provide the body of a method, while a method reference refers directly to an existing method.
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 + '}'; } public static void main(String[] args) {} public static void main(String[] args) { Integer> fun = Integer::parseInt; Integer value = fun.apply("123"); System.out.println(value); // Use double colons :: to construct a non-static function reference String content = "Hello JDK8"; Function<Integer, String> func = content::substring; String result = func.apply(1); System.out.println(result); // The constructor references BiFunction<String, Integer, User> BiFunction = User::new; User user = biFunction.apply("mengday", 28); System.out.println(user.toString()); SayHello (String::toUpperCase, "hello"); } // The method takes two arguments, Private static void sayHello(Function<String, String> func, String parameter){ String result = func.apply(parameter); System.out.println(result); }Copy the code
Three: Optional
Google Guava has Optional, and Swift has a similar syntax. In Swift, the Optional value is treated as a data type, with a high status equal to the basic type.
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<T> {private static final Optional<? > EMPTY = new Optional<>(); private final T value; private Optional() { this.value = null; Public static<T> Optional<T> empty() {@unchecked ("unchecked") Optional<T> T = (Optional<T>) EMPTY; return t; } private Optional(T value) { this.value = Objects.requireNonNull(value); } public static <T> Optional<T> of(T value) {return new Optional<>(value); } // Return an Optional, if not empty, Optional public static <T> Optional<T> ofNullable(T value) {return value == null? empty() : of(value); } // If Optional has a value, return the value, otherwise NoSuchElementException is thrown. public T get() { if (value == null) { throw new NoSuchElementException("No value present"); } return value; } // Return true if a value exists, false otherwise public Boolean isPresent() {return value! = null; } // If a value exists, the specified consumer is invoked with that value, otherwise nothing is done. public void ifPresent(Consumer<? super T> consumer) { if (value ! = null) consumer.accept(value); } // Return an Optional description if a value exists and the Predicate for the value matches, otherwise return an empty Optional public Optional<T> filter(Predicate< T> filter). super T> predicate) { Objects.requireNonNull(predicate); if (! isPresent()) return this; else return predicate.test(value) ? this : empty(); } // If there is a value, the provided mapping function is applied, and if the result is not empty, an Optional result is returned. public<U> Optional<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper); if (! isPresent()) return empty(); else { return Optional.ofNullable(mapper.apply(value)); } // If a value exists, apply the provided Optional mapping function to it and return the result, otherwise return an empty Optional. public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) { Objects.requireNonNull(mapper); if (! isPresent()) return empty(); else { return Objects.requireNonNull(mapper.apply(value)); Public T orElse(T other) {return value! Public T orElse(T other) {return value! = null ? value : other; } public T orElseGet(Supplier<? extends T> other) { return value ! = null ? value : other.get(); } public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { if (value ! = null) { return value; } else { throw exceptionSupplier.get(); }}}Copy the code
The “of” method is a static method named “of” that returns the value of the current class and makes the constructor private. The “of” method is a static method.
public class User { private String username; private Integer age; private User() { } public static User of() { return new User(); } private User(String username, Integer age) { this.username = username; this.age = age; } public static User of(String username, Integer age) { return new User(username, age); }} Main public static void Main (String[] args) {// The Optional class is part of the Java 8 class library. String MSG = "hello"; // Optional String MSG = "hello"; // Optional String MSG = "hello"; Optional<String> optional = Optional.of(msg); Boolean present = optional. IsPresent (); String value = option.get (); String value = option.get (); String hi = optional. OrElse ("hi"); // If null, return the else value. // If the value is not empty, execute the Lambda expression option.ifpresent (opt -> system.out.println (opt)); // If the value is not empty, execute the Lambda expression option.ifpresent (opt -> system.out.println (opt)); }Copy the code
Source: suo. Im / 6 vk9my
Welcome to follow my wechat public account “Code-farmer Breakthrough”, share Python, Java, big data, machine learning, artificial intelligence and other technologies, pay attention to code-farmer technology improvement, career breakthrough, thinking leap, 200,000 + code-farmer growth charge the first station, accompany you to grow with a dream