@Test
public void function(){
List<String> list = Lists.newArrayList("1","2","3");
List<Integer> integers = Lists.transform(list,new Function<String, Integer>() {
@Override
public Integer apply(String arg0) {
return Integer.valueOf(arg0);
}
});
System.out.println(integers);
}
guava 中的函数式编程,比较好用吧
@Test
public void predicate(){
List<String> list = Lists.newArrayList("1","2","3");
List<String> strings = Lists.newArrayList( Iterables.filter(list, new Predicate<String>() {
@Override
public boolean apply(String arg0) {
// TODO Auto-generated method stub
return !arg0.equals("3");
}
}));
System.out.println(strings);
}
guava 断言编程
总结一下,根据guava官网,和 function predicate 自身特点 不难找到规律。 function 常用来 转换 (transform) predicate 常用来 过滤(filter) (好像function predicate 天生如此) 等有时间了,把guava官网翻译一下,不知道有没有人需要啊