Java 8 - 14 新特性和API ---持续更新ing,安卓内存优化工具

Code:

public interface INumberFunction{

void change();

void delete();

int getNumber();

default void currentNumber(){

System.out.println(“current”);

callPrivateMethod();

}

private void callPrivateMethod(){

System.out.println(“jdk9 level const method”);

}

}

<APIS  1.8+>

Predicate:

源码:

//

// Source code recreated from a .class file by IntelliJ IDEA

// (powered by Fernflower decompiler)

//

package java.util.function;

import java.util.Objects;

@FunctionalInterface

public interface Predicate {

boolean test(T var1);

default Predicate and(Predicate<? super T> other) {

Objects.requireNonNull(other);

return (t) -> {

return this.test(t) && other.test(t);

};

}

default Predicate negate() {

return (t) -> {

return !this.test(t);

};

}

default Predicate or(Predicate<? super T> other) {

Objects.requireNonNull(other);

return (t) -> {

return this.test(t) || other.test(t);

};

}

static Predicate isEqual(Object targetRef) {

return null == targetRef ? Objects::isNull : (object) -> {

return targetRef.equals(object);

};

}

static Predicate not(Predicate<? super T> target) {

Objects.requireNonNull(target);

return target.negate();

}

}

可以理解为一个比较器  常用方法 test()  返回一个boolean 值

negate() 取反比较(相比于test) 返回当前实例 可以链式调用

![](https://img-blog.csdnimg.cn/20200529140946552.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5NzY5ODUx,size_16,color_FFFFFF,t_70)

执行结果:

false

true

Function :

源码:

//

// Source code recreated from a .class file by IntelliJ IDEA

// (powered by Fernflower decompiler)

//

package java.util.function;

import java.util.Objects;

@FunctionalInterface

public interface Function<T, R> {

R apply(T var1);

default Function<V, R> compose(Function<? super V, ? extends T> before) {

Objects.requireNonNull(before);

return (v) -> {

return this.apply(before.apply(v));

};

}

default Function<T, V> andThen(Function<? super R, ? extends V> after) {

Objects.requireNonNull(after);

return (t) -> {

return after.apply(this.apply(t));

};

}

static Function<T, T> identity() {

return (t) -> {

return t;

};

}

}

函数式操作,学习这个需要对泛型(java1.5)有一定的了解,比如:

CallBack

List<? super Android>

List<?  extends Method>

List<? super Object>

Map<K,V>

apply() :

Function<Integer,Integer> function1 = integer -> integer*=3;

System.out.println(function1.apply(6));

输出结果:

18

compose:

private static void functionMethod() {

Function<Integer, Integer> function1 = integer -> integer *= 3;

System.out.println(function1.apply(6));

Function<Integer,Integer> function4 = integer -> integer *= 3;

System.out.println(function1.compose(function4).apply(6));

Function<String, String> function2 = (user) -> user.substring(0, user.length() - 1);

System.out.println(function2.apply(“那一年 风吹半夏”));

Function<Person, Boolean> function3 = (person) -> person.getName().isBlank();

}

输出:

18

54

那一年 风吹半

supplier:

源码:

//

// Source code recreated from a .class file by IntelliJ IDEA

// (powered by Fernflower decompiler)

//

package java.util.function;

@FunctionalInterface

public interface Supplier {

T get();

}

可以看出只有一个get方法,可以理解为是一个抽象工厂模式,下面我们进行测试一下.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值