在guava库中,自带了过滤器(filter)的功能,可以用来对collection 进行过滤,先看例子:
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
Iterable<String> result = Iterables.filter(names, Predicates.containsPattern("a"));
// [Jane, Adam]
System.out.println(result);
}
}
在这个例子中,给出一个list,过滤出含有字母a的元素
此外,可以使用Collections2.filter() 去进行过滤
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
Collection<String> result = Collections2.filter(names, Predicates.containsPattern("a"));
// [Jane, Adam]
System.out.println(result);
}
}
再来看下predicates判断语言,
com.google.common.base. Predicate : 根据输入值得到 true 或者 false
拿Collections2中有2个函数式编程的接口:filter , transform ,例如 :在Collection<Integer>中过滤大于某数的内容:
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> collections = Lists.newArrayList(1, 2, 3, 4);
Collection<Integer> filter = Collections2.filter(
collections, new Predicate<Integer>() {
@Override
public boolean apply(Integer input) {
return input >= 3;
}
});
// [3, 4]
System.out.println(filter);
}
}
把Lis<Integer>中的Integer类型转换为String , 并添加test作为后缀字符
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> list = Lists.newArrayList(1, 2, 3, 4);
List<String> transform = Lists.transform(list, new Function<Integer, String>() {
@Override
public String apply(Integer input) {
return input + "_test";
}
});
// [1_test, 2_test, 3_test, 4_test]
System.out.println(transform);
}
}
需要说明的是每次调用返回都是新的对象,同时操作过程不是线程安全的。
将多个prdicate进行组合
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
Collection<String> result = Collections2.filter(names,
Predicates.or(Predicates.containsPattern("J"),
Predicates.not(Predicates.containsPattern("a"))));
// [John, Jane, Tom]
System.out.println(result);
}
}
上面的例子中找出包含J字母或不包含a的元素;
再看下如何将集合中的空元素删除:
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", null, "Jane", null, "Adam", "Tom");
Collection<String> result = Collections2.filter(names, Predicates.notNull());
// [John, Jane, Adam, Tom]
System.out.println(result);
}
}
检查一个collection中的所有元素是否符合某个条件:
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
boolean result = Iterables.all(names, Predicates.containsPattern("n|m"));
// true
System.out.println(result);
result = Iterables.all(names, Predicates.containsPattern("a"));
// false
System.out.println(result);
}
}