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