filter a collection, get a new list whose elements' property value meets values in condition - Java java.util

Java examples for java.util:Collection Filter

Description

filter a collection, get a new list whose elements' property value meets values in condition

Demo Code


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.BeanUtils;

public class Main {

  /**/*  ww  w.  jav a2 s . c o  m*/
   * filter a collection, get a new list whose elements' property value meets
   * values in condition
   * 
   * @param c
   * @param property
   * @param condition
   * @return
   */
  public static List filterAll(Collection collection, String property, Collection condition) {
    List result = new ArrayList();

    Iterator iter = collection.iterator();
    while (iter.hasNext()) {
      Object bean = iter.next();
      Object value = BeanUtils.getProperty(bean, property);
      if (condition != null && condition.contains(value)) {
        result.add(bean);
      }
    }
    return result;
  }
}

Related Tutorials