java中的?是什么意思(java中?啥意思)
本篇文章给大家谈谈
原标题:Java中的 :: 是什么鬼?作者:泡代码的小二黑 来源:https://urlify.cn/Qjaaue
方法引用分为三种,可以通过一对双冒号:: 来表示,方法引用是一种函数式接口的另一种书写方式静态方法引用,通过类名::静态方法名, 如 Integer::parseInt 实例方法引用,通过实例对象::实例方法,如 str::substring
构造方法引用,通过类名::new, 如 User::new 方法引用publicfinalclassInteger{ publicstaticintparseInt(String s)throwsNumberF微信引流推广平台ormatException
{ returnparseInt(s, 10); } } 通过方法引用,可以将方法的引用赋值给一个变量,通过赋值给Function,说明方法引用也是一种函数式接口的书写方式,Lambda表达式也是一种函数式接口,Lambda表达式一般用于自己提供方法体,而方法引用一般直接引用现成的方法。
publicclassUser{ privateString username; privateInteger age; publicUser{ } publicUser(String username, Integer age)
{ this.username = username;微信引流推广平台 this.age = age; } @OverridepublicString toString{ return"User{"+ "username="
+ username + \+ ", age="+ age + }; } // Getter&Setter} publicstaticvoidmain(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(Str微信引流推广平台ing::toUpperCase, "hello"); } // 方法有两个参数,一个是privatestaticvoidsayHello(Function func, String parameter)
{ String result = func.apply(parameter); System.out.println(result); } Optional 可选值在Google Guava 中就有Optional,在Swift语言中也有这样类似的语法,在Swift中将可选值作为一种数据类型,地位和基本类型平齐平做,地位非常高。
packagejava.util; importjava.uti微信引流推广平台l.function.Consumer; importjava.util.function.Function; importjava.util.function.Predicate;
importjava.util.function.Supplier; /** * @since1.8 */ publicfinalclassOptional { privatestaticfinal
Optional EMPTY = newOptional<>; privatefinalT value; privateOptional{ this.value = null; } // 返回一个空的 Optional实微信引流推广平台例
publicstatic Optional empty{ @SuppressWarnings( "unchecked") Optional t = (Optional) EMPTY;
returnt; } privateOptional(T value){ this.value = Objects.requireNonNull(value); } // 返回具有 Optional的当前非空值的Optional
publicstatic Optional of(T value){ returnnewOptional<>(value); } // 返回一个 Optional指定值的Optional,微信引流推广平台如果非空,则返回一个空的 Optional
publicstatic Optional ofNullable(T value){ returnvalue == null? empty : of(value); } // 如果Optional中有一个值,返回值,否则抛出 NoSuchElementException 。
publicT get{ if(value == null) { thrownewNoSuchElementException( "No value present"); } returnvalue;
} // 返回true如果存在值,否则为 false publicbooleanis微信引流推广平台Present{ returnvalue != null; } // 如果存在值,则使用该值调用指定的消费者,否则不执行任何操作。
publicvoidifPresent(Consumer consumer) { if(value != null) consumer.accept(value); } // 如果一个值存在,并且该值给定的谓词相匹配时,返回一个 Optional描述的值,否则返回一个空的 Optional
publicOptional filter(Predicate predicate) { Objects.requireNonNull(predicate); if(!isPres微信引流推广平台ent)
returnthis; elsereturnpredicate.test(value) ? this: empty; } // 如果存在一个值,则应用提供的映射函数,如果结果不为空,则返回一个 Optional结果的 Optional 。
public Optional map(Function mapper) { Objects.requireNonNull(mapper); if
(!isPresent) returnempty; else{ returnOptional.ofNullable(mapper.apply(value)); } } // 如果一个值存在,应用提供的 Opt微信引流推广平台ional映射函数给它,返回该结果,否则返回一个空的 Optional 。
public Optional flatMap(Function mapper) { Objects.requireNonNull(mapper);
if(!isPresent) returnempty; else{ returnObjects.requireNonNull(mapper.apply(value)); } } // 如果值存在,就返回值,不存在就返回指定的其他值
publicT orElse(T other){ returnvalue != null? value : other; } pub微信引流推广平台licT orElseGet(Supplier other)
{ returnvalue != null? value : other.get; } public T orElseThrow(Supplier exceptionSupplier)
throwsX { if(value != null) { returnvalue; } else{ throwexceptionSupplier.get; } } } 关于of方法,现在好像很流行,就是提供一个static方法,方法名称叫of,方法的返回值返回当前类,并且把构造函数设置为私有private,用静态of方法来代替构造函数。
publiccl微信引流推广平台assUser{ privateString username; privateInteger age; privateUser{ } publicstaticUser of{ return
newUser; } privateUser(String username, Integer age){ this.username = username; this.age = age; } public
staticUser of(String username, Integer age){ returnnewUser(username, age); } } Mainpublicstaticvoidma微信引流推广平台in
(String[] args){ // Optional类已经成为Java 8类库的一部分,在Guava中早就有了,可能Oracle是直接拿来使用了// Optional用来解决空指针异常,使代码更加严谨,防止因为空指针NullPointerException对代码造成影响
String msg = "hello"; Optional optional = Optional.of(msg); // 判断是否有值,不为空booleanpresent = optional.isPresent;
// 如果有值,则返回值,如果等于空则抛异常String value = optional.get; /微信引流推广平台/ 如果为空,返回else指定的值String hi = optional.orElse( "hi"
); // 如果值不为空,就执行Lambda表达式optional.ifPresent(opt -> System.out.println(opt)); } • 面试官:聊一聊Java 泛型通配符 T,E,K,V,?
• 阿里一面:如何保证API接口数据安全? • 0.2秒居然复制了100G文件? • Java17,有史以来最快 JDK! 最近写了一套 6000 页的 Java 学习手册,以及珍藏四本 Java 人必读4大
神器,分享到知乎已经 3 万赞了!每篇文章图文并茂,附有源码还有电子书合集如微信引流推广平台果你想获得完整PDF可以通过以下方式获得资料获取方法即可领取在后台回复关键词 002明天见(。・ω・。)返回搜狐,查看更多责任编辑: