我就吃瓜(Guava): Functions&Predicates

Functions和Predicates介绍和使用

Functions: 主要用于产生各种Function

  • Function 可以进行函数式编程,其中的数据也以key_value的形式进行存储

    1. Function作为一个接口,主要使用的是apply()方法,进行对象转换

    2. 初始化一个Function 需要重写apply()方法

  • Functions 主要用于对Function的使用

    1. forMap(), 将Map实例转换为Function实例

    2. toStringFunction(), 产生一个Function<Object, String>实例,String 为Object对象的toString()方法返回值,两者一一对应。

    3. compose(), 参数是两个Function实例。

@Test
public void test1() {
    Map<String, Long> stringLong = new HashMap<String, Long>();
    stringLong.put("key1", 1L);
    stringLong.put("key2", 2L);
    //将Map实例转换为Function实例
    Function<String, Long> stringLongFunction = Functions.forMap(stringLong, 0L);
    // 1
    System.out.println(stringLongFunction.apply("key1"));
	/* 第一个Function是定义转换规则,对第二个Function的value进行处理,
	 * 下面栗子里,用新建的Function对stringLongFunction的value进行处理,规则就是重写的apply()方法
	 */
    Function<String, String> compose = Functions.compose(new Function<Long, String>() {
        @Override
        public String apply(Long input) {
            return input * input + "";
        }
    }, stringLongFunction);
    Function<String, String> compose1 = Functions.compose(new Function<String, String>() {
        @Override
        public String apply(String input) {
            return input + input;
        }
    }, compose);
	// 44 = "2*2" + "2*2"
    System.out.println(compose1.apply("key2"));
}

Predicates: 主要用于产生各种Predicate

  • Predicate 断言。

    1. 对参数进行判断,判断依据是重写的apply()方法,返回结果是布尔值

    2. 初始化一个Predicate 需要重写apply()方法

  • Predicates 产生各种各样的Predicate

    1. alwaysTrue()和alwaysFalse() 永远返回true或者false

    2. isNull()和notNull() 根据是否为空返回true和false

    3. and(Predicate A, Predicate B) 和 or(Predicate A, Predicate B) 两个断言的交集或者并集

    4. Predicates.compose(Predicate A, FunctionA); 对FunctionA 的value进行判断,判断规则是

    Predicate B,查看时通过key来进行查看。

@Test
public void test3() {
    Map<String, Long> stringLong = new HashMap<String, Long>();
    stringLong.put("key1", 1L);
    stringLong.put("key3", 3L);
    Function<String, Long> stringLongFunction = Functions.forMap(stringLong, 0L);
    //true,false
    System.out.println(Predicates.alwaysTrue().apply(1));
    System.out.println(Predicates.isNull().apply(1));

    //多个组合
    Predicate<Object> or = Predicates.or(Predicates.isNull(), Predicates.notNull());
    //true,true
    System.out.println(or.apply(null));
    System.out.println(or.apply("1"));
    Predicate<Long> and = Predicates.and(new Predicate<Long>() {
        @Override
        public boolean apply(Long input) {
            return input < 5;
        }
    }, new Predicate<Long>() {
        @Override
        public boolean apply(Long input) {
            return input > 2;
        }
    });
    Predicate<String> compose = Predicates.compose(and, stringLongFunction);
    System.out.println("--------Predicate----------");
    //false,true
    System.out.println(compose.apply("key1"));
    System.out.println(compose.apply("key3"));

    //转换为Function
    Function<Long, Boolean> longBoolean = Functions.forPredicate(and);
    System.out.println("--------Predicate-Function---------");
    //true,false
    System.out.println(longBoolean.apply(3L));
    System.out.println(longBoolean.apply(6L));

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值