1. 程式人生 > >Guava中針對集合的 filter和過濾功能

Guava中針對集合的 filter和過濾功能

操作 rri 根據 lis name 說明 ase 不包含 all

在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);
    }
}

Guava中針對集合的 filter和過濾功能