Function, Predicate
Function, Predicate的使用
Function用于把一种类型的对象转化为另一种类型的对象.
Predicate用于判断某个对象是否符合一定条件.
一.Function和Functions
在java开发中, 我们经常需要对一些对象进行处理, 然后返回我们想要的结果, 比如说:对日期进行格式化, 获取字符串等等.在guava中, 我们可以通过实现Function接口来实现类似的需求, 如下:
import com.google.common.base.Function;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by xinfengyao on 16-2-22.
*/
public class FunctionTest {
public static void main(String[] args) {
Function<Date, String> function = new Function<Date, String>() {
@Override
public String apply(Date date) {
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
};
System.out.println(function.apply(new Date()));
}
}
打印结果为:
2016-02-22
我们看到, 调用Function接口中的apply()方法, 将我们想要处理的对象作为输入参数, 返回结果就是处理后我们想要的. Function接口中, 其实有两个方法, 我们一般只关心apply()方法.
package com.google.common.base;
import com.google.common.annotations.GwtCompatible;
import javax.annotation.Nullable;
@GwtCompatible
public interface Function<F, T> {
@Nullable
T apply(@Nullable F var1);
boolean equals(@Nullable Object var1);
}
多数情况下, 我们需要自己实现Function接口, 以满足不同的需求. 不过Guava提供了工具类Functions, 其中包含了一些常用的Function实现. Functions中最重要的两个方法是forMap()和compose(). 通过实例代码, 学习这两个方法:
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.Maps;
import java.util.Map;
/**
* Created by xinfengyao on 16-2-22.
*/
public class FunctionsTest {
public static void main(String[] args) {
Map<String, Integer> map = Maps.newHashMap();
map.put("aaa", 1);
map.put("bbb", 2);
Function<String, Integer> function = Functions.forMap(map);
/**
* 调用apply()方法, 可以通过key获取对应的value
* 当传给apply()中的key在map中不存在时, 就会抛出异常IllegalArgumentException
*/
System.out.println(function.apply("aaa"));
/**
* 我们可以通过forMap()另一个重载方法避免抛出异常, 当key不存在时, 设置一个默认值
*/
function = Functions.forMap(map, 0);
System.out.println(function.apply("ccc"));
/**
* 有时候我们需要通过多个Function进行组合, 这时我们就需要用到compose()方法
*/
Function<Integer, Integer> function2 = new Function<Integer, Integer>() {
@Override
public Integer apply(Integer input) {
return input * input;
}
};
/**
* 将上面map中的值进行平方运算, 注意compose()方法中的输入参数的顺序不能颠倒:
* Function<A, C> compose(Function<B, C> g, Function<A, ? extends B> f)
*/
Function<String, Integer> result = Functions.compose(function2, function);
System.out.println(result.apply("bbb"));
}
}
运行结果:
1 0 4
通过运行结果, 我们能够看到compose()方法的内部执行过程: 假定Fuctions.compose()方法返回的Function为function1, compose()的第一个参数为function2, 第二个参数为function3, 那么调用function1.apply()方法时, 输入参数会传给function3的apply()方法, function3.apply()方法的返回结果作为function2.apply()方法的入参, function2.apply()方法的返回结果就是function1.apply()方法的执行结果.
二.Predicate和Predicates
如果说Function主要用于对象的转换, 那么Predicate则主要用作对象的过滤和筛选. Predicate和Function一样, 也有两个方法, 我们主要关心apply()方法.
package com.google.common.base;
import com.google.common.annotations.GwtCompatible;
import javax.annotation.Nullable;
@GwtCompatible
public interface Predicate<T> {
boolean apply(@Nullable T var1);
boolean equals(@Nullable Object var1);
}
通过下面这个例子学习Predicate的使用:
/**
* Created by xinfengyao on 16-2-22.
*/
public class Person {
private String name;
private int age;
private int height;
// 省略getter和setter方法, 构造方法
}
import com.google.common.base.Predicate;
/**
* Created by xinfengyao on 16-2-22.
*/
public class PredicateTest {
public static void main(String[] args) {
Predicate<Person> predicate = new Predicate<Person>() {
@Override
public boolean apply(Person person) {
return person.getAge() > 18;
}
};
Person person = new Person("tom", 23, 175);
System.out.println(predicate.apply(person));
}
}
打印结果:
true
与Function类似, Predicate也有对应的工具类Predicates. 下面来学习几个比较重要的方法: Predicates.not(), Predicates.or(), Predicates.and(), Predicates.compose()
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Maps;
import java.util.Map;
/**
* Created by xinfengyao on 16-2-22.
*/
public class PredicatesTest {
public static void main(String[] args) {
Predicate<Person> predicate1 = new Predicate<Person>() {
@Override
public boolean apply(Person person) {
return person.getAge() > 18;
}
};
Predicate<Person> predicate2 = new Predicate<Person>() {
@Override
public boolean apply(Person person) {
return person.getHeight() > 175;
}
};
Person person = new Person("Jack", 25, 170);
Predicate<Person> predicate3 = Predicates.and(predicate1, predicate2);
System.out.println(predicate3.apply(person));
Predicate<Person> predicate4 = Predicates.not(predicate1);
System.out.println(predicate4.apply(person));
Predicate<Person> predicate5 = Predicates.or(predicate1, predicate2);
System.out.println(predicate5.apply(person));
Map<String, Person> personMap = Maps.newHashMap();
personMap.put("marry", new Person("marry", 21, 165));
personMap.put("bob", new Person("bob", 25, 180));
Predicate<String> predicate6 = Predicates.compose(predicate2, Functions.forMap(personMap));
System.out.println(predicate6.apply("marry"));
}
}
运行结果:
false false true false
Predicates.compose()方法运行过程: 假定Predicates.compose()返回predicate1, compose()的第一个参数为predicate2, 第二个参数为function1; 则当调用predicate1.apply()方法时, 输入参数传递给function1, function1的返回结果作为predicate2的输入参数, predicate2的返回结果就是predicate1.apply()的执行结果.
Function, Predicate的更多相关文章
- guava function and predicate 函数式编程
@Test public void function(){ List<String> list = Lists.newArrayList("1","2&quo ...
- java8 array、list操作 汇【2】)- (Function,Consumer,Predicate,Supplier)应用
static class UserT { String name; public UserT(String zm) { this.name=zm; } public String getName() ...
- 函数式接口的使用 (Function、Predicate、Supplier、Consumer)
参考:https://blog.csdn.net/jmj18756235518/article/details/81490966 函数式接口 定义:有且只有一个抽象方法的接口 Function< ...
- Java8-四个函数式接口(Consumer,Supplier,Predicate,Function)
Java8---函数式接口 Consumer---消费者(accept方法,Lambda与方法引用返回都是Consumer) Supplier---供给型(get方法,返回数据,与Optional可以 ...
- Predicate接口和Consumer接口
Predicate和Consumer接口是集合操作中常用到的俩个接口: 1.Predicate public interface Predicate<T>{ boolean test(T ...
- Java 8 基础教程 - Predicate
在Java 8中,Predicate是一个函数式接口,可以被应用于lambda表达式和方法引用.其抽象方法非常简单: /** * Evaluates this predicate on the giv ...
- Java 8 新特性:4-断言(Predicate)接口
(原) 这个接口主要用于判断,先看看它的实现,说明,再给个例子. /* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All ri ...
- Java8 Predicate
code: package com.qhong; import java.util.ArrayList; import java.util.Arrays; import java.util.List; ...
- STL - Predicate - Binary Predicate(双参判断式)
Binary Predicate(双参判断式)的用途是:比较两个参数的特定属性 我们先建一个领域模型类: Person.h #ifndef _Domain_Models_Person_H_ #defi ...
随机推荐
- PostgreSQL Replication之第十章 配置Slony(6)
10.6 执行故障切换 一旦您学会了如何复制表并将它们添加到集合中,是时候学习故障转移了.基本上,我们可以在两个两种类型的故障转移之间做出区分: • 计划内故障转移 • 计划外故障转移和崩溃 在本节, ...
- 事件监听addEventListener()和removeEventListener() ---------1
一直想写一个原生事件监听的记录,方便以后看,不愿意写主要是事件监听的单词太长,记起来好难记每次都要查,这次把知道的写完了,来这里查好了,以后要是理解的更透彻了,就再补全好了 首先,DOM0级事件和DO ...
- Leetcode: UTF-8 Validation
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte char ...
- Codeforce Round #217 Div2
e,妈蛋,第二题被hack了 没理解清题意,- -居然也把pretest过了,- -# A: 呵呵! B:包含任意一个子集的输出NO!,其他输出YES! C:贪心额,类似上次的Topcoder的500 ...
- 最近兰州的js风格写个插件和一个template engine
/* *@Product Name: Rational Framework Author: Calos Description: pager !important: pager */ (functio ...
- codevs 1201 最小数和最大数
http://codevs.cn/problem/1201/ 1201 最小数和最大数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 题 ...
- POJ 3356 AGTC(DP-最小编辑距离)
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- URAL 1161 Stripies(数学+贪心)
Our chemical biologists have invented a new very useful form of life called stripies (in fact, they ...
- zw版_Halcon图像库delphi接口文件
zw版_Halcon图像库delphi接口文件 Halcon图像库delphi接口文件,根据安装时用户设置的文件目录不同,会有所差异,笔者一般安装在delphi的import目录下. 参见:& ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON BinThreshold
zw版[转发·台湾nvp系列Delphi例程]HALCON BinThreshold unit Unit1;interfaceuses Windows, Messages, SysUtils, Var ...