在guava庫中,自帶了過濾器(filter)的功能,可以用來對collection 進行過濾,先看例子:
- @Test
- public void whenFilterWithIterables_thenFiltered() {
- List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
- Iterable<String> result = Iterables.filter(names, Predicates.containsPattern("a"));
- assertThat(result, containsInAnyOrder("Jane", "Adam"));
- }
在這個例子中,給出一個list,過濾出含有字母a的元素
此外,可以使用Collections2.filter() 去進行過濾
- @Test
- public void whenFilterWithCollections2_thenFiltered() {
- List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
- Collection<String> result = Collections2.filter(names, Predicates.containsPattern("a"));
- assertEquals(2, result.size());
- assertThat(result, containsInAnyOrder("Jane", "Adam"));
- result.add("anna");
- assertEquals(5, names.size());
- }
這裏注意的是,Collections2.filter中,當在上面的result中增加了元素後,會直接影響原來的names這個list的,就是names中的集合元素是5了。
再來看下predicates判斷語言,
com.google.common.base. Predicate : 根據輸入值得到 true 或者 false
拿Collections2中有2個函數式編程的接口:filter , transform ,例如 :在Collection<Integer>中過濾大於某數的內容:
- Collection<Integer> filterList = Collections2.filter(collections
- , new Predicate<Integer>(){
- @Override
- public boolean apply(Integer input) {
- if(input > 4)
- return false;
- else
- return true;
- }
- });
把Lis<Integer>中的Integer類型轉換爲String , 並添加test作爲後綴字符:
- List<String> c2 = Lists.transform(list, new Function<Integer , String>(){
- @Override
- public String apply(Integer input) {
- return String.valueOf(input) + "test";
- }
- });
需要說明的是每次調用返回都是新的對象,同時操作過程不是線程安全的。
再來點例子:
- @Test
- public void whenFilterCollectionWithCustomPredicate_thenFiltered() {
- Predicate<String> predicate = new Predicate<String>() {
- @Override
- public boolean apply(String input) {
- return input.startsWith("A") || input.startsWith("J");
- }
- };
- List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
- Collection<String> result = Collections2.filter(names, predicate);
- assertEquals(3, result.size());
- assertThat(result, containsInAnyOrder("John", "Jane", "Adam"));
- }
將多個prdicate進行組合
- @Test
- public void whenFilterUsingMultiplePredicates_thenFiltered() {
- 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"))));
- assertEquals(3, result.size());
- assertThat(result, containsInAnyOrder("John", "Jane", "Tom"));
- }
上面的例子中找出包含J字母或不包含a的元素;
再看下如何將集合中的空元素刪除:
- @Test
- public void whenRemoveNullFromCollection_thenRemoved() {
- List<String> names = Lists.newArrayList("John", null, "Jane", null, "Adam", "Tom");
- Collection<String> result = Collections2.filter(names, Predicates.notNull());
- assertEquals(4, result.size());
- assertThat(result, containsInAnyOrder("John", "Jane", "Adam", "Tom"));
- }
檢查一個collection中的所有元素是否符合某個條件:
- @Test
- ublic void whenCheckingIfAllElementsMatchACondition_thenCorrect() {
- List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
- boolean result = Iterables.all(names, Predicates.containsPattern("n|m"));
- assertTrue(result);
- result = Iterables.all(names, Predicates.containsPattern("a"));
- assertFalse(result);
下面看如何把一個list進行轉換,
- @Test
- public void whenTransformWithIterables_thenTransformed() {
- Function<String, Integer> function = new Function<String, Integer>() {
- @Override
- public Integer apply(String input) {
- return input.length();
- }
- };
- List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
- Iterable<Integer> result = Iterables.transform(names, function);
- assertThat(result, contains(4, 4, 4, 3));
- }
再看結合transform和predicates結合使用的例子:
- @Test
- public void whenCreatingAFunctionFromAPredicate_thenCorrect() {
- List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
- Collection<Boolean> result =
- Collections2.transform(names,
- Functions.forPredicate(Predicates.containsPattern("m")));
- assertEquals(4, result.size());
- assertThat(result, contains(false, false, true, true));
- }
在這個例子中,將一個LIST中的每一個元素進行使用Predicates.containsPattern,判斷是否包含m,返回的是boolean,然後再得到的boolean值一起轉換爲collection
下面是兩個function一起結合使用的例子:
- @Test
- public void whenTransformingUsingComposedFunction_thenTransformed() {
- Function<String,Integer> f1 = new Function<String,Integer>(){
- @Override
- public Integer apply(String input) {
- return input.length();
- }
- };
- Function<Integer,Boolean> f2 = new Function<Integer,Boolean>(){
- @Override
- public Boolean apply(Integer input) {
- return input % 2 == 0;
- }
- };
- List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
- Collection<Boolean> result = Collections2.transform(names, Functions.compose(f2, f1));
- assertEquals(4, result.size());
- assertThat(result, contains(true, true, true, false));
- }
在這個例子中,首先應用函數f1,求出每個元素的長度,然後再根據f1函數,分別返回
它們的boolean值,再轉換爲collection.
最後看下將filter和transform結合使用的例子:
- @Test
- public void whenFilteringAndTransformingCollection_thenCorrect() {
- Predicate<String> predicate = new Predicate<String>() {
- @Override
- public boolean apply(String input) {
- return input.startsWith("A") || input.startsWith("T");
- }
- };
- Function<String, Integer> func = new Function<String,Integer>(){
- @Override
- public Integer apply(String input) {
- return input.length();
- }
- };
- List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
- Collection<Integer> result = FluentIterable.from(names)
- .filter(predicate)
- .transform(func)
- .toList();
- assertEquals(2, result.size());
- assertThat(result, containsInAnyOrder(4, 3));
- }