首页 > 代码库 > guava学习--Function、Predicate
guava学习--Function、Predicate
Function用于同步转换。
Predicate用于过滤。
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class User {
private String username;
private String sex;
public User(String username, String sex) {
this.username = username;
this.sex = sex;
}
void setUsername(String username) {
this.username = username;
}
void setSex(String sex) {
this.sex = sex;
}
String getUsername() {
return username;
}
String getSex() {
return sex;
}
@Override
public String toString() {
return Objects.toStringHelper(this).omitNullValues().add("username", username).add("sex", sex).toString();
}
public static void main(String[] args) {
User u1 = new User("malone", "man");
User u2 = new User("lanlan", "woman");
List<User> list = Lists.newArrayList(u1, u2);
// Function 这里用于类型转换
Collection<String> result = Collections2.transform(list, new Function<User, String>() {
@Override
public String apply(User user) {
if (Objects.equal(user, null)) {
return "";
}
return user.toString();
}
});
for (String ss : result) {
System.out.println(ss);
}
// Predicate 这里是用于过滤
Collection<User> resultUser = Collections2.filter(list, new Predicate<User>() {
@Override
public boolean apply(User user) {
if (Objects.equal(user, null)) {
return false;
}
if (Objects.equal(user.getSex(), "woman")) {
return false;
}
return true;
}
});
for (User ss : resultUser) {
System.out.println(ss);
}
Function<User, String> f1 = new Function<User, String>() {
@Override
public String apply(User user) {
if (Objects.equal(user, null)) {
return "";
}
return user.getUsername();
}
};
// 多个工具类均可使用类函数式编程
Collection<String> result1 = Collections2.transform(list, f1);
for (String ss : result1) {
System.out.println("1:" + ss);
}
List<String> strList = Lists.transform(list, f1);
for (String ss : strList) {
System.out.println("2:" + ss);
}
Iterator<String> it = Iterators.transform(list.iterator(), f1);
while (it.hasNext()) {
System.out.println("3:" + it.next());
}
Function<String, String> f2 = new Function<String, String>() {
@Override
public String apply(String input) {
if (Objects.equal(input, "malone")) {
return "malone‘s function";
} else {
return "lanlan‘s function";
}
}
};
// 连接多个Function
// Warning:这里compose方法的参数位置不能颠倒,
// * Function<A, C> compose(Function<B, C> g, Function<A, ? extends B> f)
// * 传入Function<B,C>、Function<A, ? extends B>组合成Function<A, C>
Function<User, String> f3 = Functions.compose(f2, f1);
List<String> mylist = Lists.transform(list, f3);
System.out.println(mylist.get(0));
Map<String, String> map = Maps.newHashMap();
map.put("1", "2");
// Functions可以转换一下map,使用语义更直白的代码,并提供默认值支持
Function<String, String> lookup = Functions.forMap(map, "default value");
System.out.println(lookup.apply("1"));
}
}
guava学习--Function、Predicate