Commons Collections 简明教程

Commons Collections - Filtering Objects

Apache Commons Collections 库的 CollectionUtils 类提供各种实用方法,用于涵盖广泛使用场景的常见操作。它有助于避免编写样板代码。在 jdk 8 之前,该库非常有用,因为 Java 8 的 Stream API 中现在提供了类似的功能。

CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This library is very useful prior to jdk 8 as similar functionalities are now provided in Java 8’s Stream API.

filter() method

可使用 CollectionUtils 的 filter() 方法,过滤列表以删除不满足由传递的谓词提供的条件的对象。

filter() method of CollectionUtils can be used to filter a list to remove objects which do not satisfy condition provided by predicate passed.

Declaration

以下是声明:

Following is the declaration for

org.apache.commons.collections4.CollectionUtils.filter() 方法 −

org.apache.commons.collections4.CollectionUtils.filter() method −

public static <T> boolean filter(Iterable<T> collection,
   Predicate<? super T> predicate)

Parameters

  1. collection − The collection to get the input from, may not be null.

  2. predicate − The predicate to use as a filter, may be null.

Return Value

如果此次调用修改了集合,则返回 True,否则返回 False。

True if the collection is modified by this call, false otherwise.

Example

以下示例显示 org.apache.commons.collections4.CollectionUtils.filter() 方法的用法。我们将过滤一个整数列表,以仅获取偶数。

The following example shows the usage of org.apache.commons.collections4.CollectionUtils.filter() method. We’ll filter a list of integer to get even numbers only.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;

public class CollectionUtilsTester {
   public static void main(String[] args) {
      List<Integer> integerList = new ArrayList<Integer>();
      integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8));
      System.out.println("Original List: " + integerList);
      CollectionUtils.filter(integerList, new Predicate<Integer>() {
         @Override
         public boolean evaluate(Integer input) {
            if(input.intValue() % 2 == 0) {
               return true;
            }
            return false;
         }
      });
      System.out.println("Filtered List (Even numbers): " + integerList);
   }
}

Output

它将产生以下结果 −

It will produce the following result −

Original List: [1, 2, 3, 4, 5, 6, 7, 8]
Filtered List (Even numbers): [2, 4, 6, 8]

filterInverse() method

可使用 CollectionUtils 的 filterInverse() 方法,过滤列表以删除满足由传递的谓词提供的条件的对象。

filterInverse() method of CollectionUtils can be used to filter a list to remove objects, which satisfy condition provided by predicate passed.

Declaration

以下是声明:

Following is the declaration for

org.apache.commons.collections4.CollectionUtils.filterInverse() 方法 −

org.apache.commons.collections4.CollectionUtils.filterInverse() method −

public static <T> boolean filterInverse(Iterable<T> collection, Predicate<? super T> predicate)

Parameters

  1. collection − The collection to get the input from, may not be null.

  2. predicate − The predicate to use as a filter, may be null.

Return Value

如果此次调用修改了集合,则返回 True,否则返回 False。

True if the collection is modified by this call, false otherwise.

Example

以下示例显示 org.apache.commons.collections4.CollectionUtils.filterInverse() 方法的用法。我们将过滤一个整数列表,以仅获取奇数。

The following example shows the usage of org.apache.commons.collections4.CollectionUtils.filterInverse() method. We’ll filter a list of integer to get odd numbers only.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;

public class CollectionUtilsTester {
   public static void main(String[] args) {
      List<Integer> integerList = new ArrayList<Integer>();
      integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8));
      System.out.println("Original List: " + integerList);
      CollectionUtils.filterInverse(integerList, new Predicate<Integer>() {
         @Override
         public boolean evaluate(Integer input) {
            if(input.intValue() % 2 == 0) {
               return true;
            }
            return false;
         }
      });
      System.out.println("Filtered List (Odd numbers): " + integerList);
   }
}

Output

结果如下所述 −

The result is as stated below −

Original List: [1, 2, 3, 4, 5, 6, 7, 8]
Filtered List (Odd numbers): [1, 3, 5, 7]