In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
本篇文章为大家展示了JDK1.8新特性之方法引用和Optiona的示例分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
一:方法引用public final class Integer { public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }}
通过方法引用,可以将方法的引用赋值给一个变量,通过赋值给Function,说明方法引用也是一种函数式接口的书写方式,Lambda表达式也是一种函数式接口,Lambda表达式一般用于自己提供方法体,而方法引用一般直接引用现成的方法。
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) { // 使用双冒号::来构造静态函数引用 Function fun = Integer::parseInt; Integer value = fun.apply("123"); System.out.println(value); // 使用双冒号::来构造非静态函数引用 String content = "Hello JDK8"; Function func = content::substring; String result = func.apply(1); System.out.println(result); // 构造函数引用 BiFunction biFunction = User::new; User user = biFunction.apply("mengday", 28); System.out.println(user.toString()); // 函数引用也是一种函数式接口,所以也可以将函数引用作为方法的参数 sayHello(String::toUpperCase, "hello");}// 方法有两个参数,一个是private static void sayHello(Function func, String parameter){ String result = func.apply(parameter); System.out.println(result);}二:Optional 可选值
在Google Guava 中就有Optional,在Swift语言中也有这样类似的语法,在Swift中将可选值作为一种数据类型,地位和基本类型平齐平做,地位非常高。
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; } // 返回一个空的 Optional实例 public static Optional empty() { @SuppressWarnings("unchecked") Optional t = (Optional) EMPTY; return t; } private Optional(T value) { this.value = Objects.requireNonNull(value); } // 返回具有 Optional的当前非空值的Optional public static Optional of(T value) { return new Optional(value); } // 返回一个 Optional指定值的Optional,如果非空,则返回一个空的 Optional public static Optional ofNullable(T value) { return value == null ? empty() : of(value); } // 如果Optional中有一个值,返回值,否则抛出 NoSuchElementException 。 public T get() { if (value == null) { throw new NoSuchElementException("No value present"); } return value; } // 返回true如果存在值,否则为 false public boolean isPresent() { return value != null; } // 如果存在值,则使用该值调用指定的消费者,否则不执行任何操作。 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.