combine property value of elements in collection, return a String List - Java java.util

Java examples for java.util:Collection Element

Description

combine property value of elements in collection, return a String List

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 {

  /**/*from   w  ww .  ja va 2s. c o  m*/
   * combine property value of elements in collection, return a String List
   * 
   * @param collection
   * @param properties
   * @param delim
   * @return
   */
  public static List combineProperties(Collection collection, String[] properties, String delim) {
    List result = new ArrayList();
    for (Iterator iter = collection.iterator(); iter.hasNext();) {
      Object bean = iter.next();
      String element = "";
      for (int i = 0; i < properties.length; i++) {
        if (i > 0) {
          element += delim;
        }
        Object value = BeanUtils.getProperty(bean, properties[i]);
        if (value != null) {
          element += value;
        }
      }
      result.add(element);
    }
    return result;
  }
}

Related Tutorials