11

Possible Duplicate:
How to convert a Java object (bean) to key-value pairs (and vice versa)?

What is the best way to convert a List<POJO> to a List<Map<K,V>>. Is there a custom method/ API?

K = field name of the POJO and V is the corresponding value

public class POJO implements Serializable{

String name;
String age;
//getters and setters
}
Community
  • 1
  • 1
Victor
  • 16,609
  • 71
  • 229
  • 409

6 Answers6

9

Sounds like a job for the good and old Introspector.

Working example:

// Don't be lazy like this, do something about the exceptions
public static void main(String[] args) throws Exception {
    List<POJO> pojos = new ArrayList<POJO>();
    POJO p1 = new POJO();
    p1.setAge("20");
    p1.setName("Name");
    pojos.add(p1);
    POJO p2 = new POJO();
    // ...
    System.out.println(convertCollection(pojos));
}

public static List<Map<String, ?>> convertCollection(Collection collection) 
        throws Exception {
    List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
    for (Object element : collection) {
        list.add(getValues(element));
    }
    return list;
}

public static Map<String, ?> getValues(Object o) 
        throws Exception {
    Map<String, Object> values = new HashMap<String, Object>();
    BeanInfo info = Introspector.getBeanInfo(o.getClass());
    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        // This will access public properties through getters
        Method getter = pd.getReadMethod();
        if (getter != null)
            values.put(pd.getName(), getter.invoke(o));
    }
    return values;
}
ktulinho
  • 3,870
  • 9
  • 28
  • 35
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
6

BeanMap from Apache Commons does the job

Omnaest
  • 3,096
  • 1
  • 19
  • 18
  • Yes. I found the answer : http://stackoverflow.com/questions/3215538/generate-mapstring-string-from-pojo – Victor May 20 '11 at 17:23
1

You can use reflection to do that. See Class.getDeclaredFields. That will give you the fields of a class, you can then get the values from them and populate your map.

Note that you might need to invoke setAccessible on the fields if the are private before you can get the value.

Edit: My answer only applies to the case where you don't know the fields / implementation of the POJO when you build the map.

Kaj
  • 10,862
  • 2
  • 33
  • 27
  • This would also work. The main reasons for going with a property Instropector over pure reflection API if all you want are public properties are: - Performance (BeanInfo gets cached, and you can even write your own to customize the results) - Getters may not reflect their private fields (or even have private fields) - Shorter code. – Anthony Accioly May 20 '11 at 18:53
1

If you mean a map of K,V then this will work

List<Pojo> pojos = ...;
Map<String, String> map = new HashMap<String,String>();
for (Pojo pojo : pojos) {
 map.put(pojo.getName(), pojo.getAge());
}
sksamuel
  • 16,154
  • 8
  • 60
  • 108
0

Assuming you want a Map sans list, the easiest way is probably with a simple for loop:

Map<K,V> theMap = new HashMap<K,V>();
for (POJO obj : theList) {
    // Obviously the below can be simplified to one line
    // but it makes sense to make everything explicit for
    // ease of reading
    K key = ... // obj.getName() maybe?
    V value = ... // obj itself maybe?
    theMap.put(key, value);
}
Adam Batkin
  • 51,711
  • 9
  • 123
  • 115
-1

You need to know the POJO name. Assuming you have something like pojo.getName(), then it goes like this:

Map<String, Pojo> pojoMap = new HashMap<String, Pojo>();
for (Pojo pojo:pojoList) {
  pojoMap.put(pojo.getName(), pojo);
}

Note that I changed your requirement, I've converted one list of pojos to one map.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268