Extract List of Property / Field from List of Objects

Objective: To get list of a property from list of objects

Case:
Many times while working with database entities or beans we have a list of beans/entities though we need only list of ids or list of other field.
So we need to iterate from list and populate desired list.

To reduce these effort, a generic method that will do this job.

We have to pass list of beans and name of field we need.

Important: Bean class must have getter method for field need to be extracted

Here is simple method:

package com.playjava.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author abhishek.anne
* @publish @ playjava.wordpress.com
*/
public class ReflectionUtil {

/**
* This method returns list of a field from list of inputlist
* @author playjava.wordpress.com
* @param inputlist
* @param field
* @return
* @throws IntrospectionException
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static List ExtractPropertyList (List inputlist,String field) throws IntrospectionException, NoSuchMethodException,IllegalAccessException, IllegalArgumentException, InvocationTargetException{
List returnList= null;
if(inputlist!=null && !inputlist.isEmpty()){
Class clazz= (Class) inputlist.get(0).getClass();
Method method=findGetter(clazz,field);
returnList= new ArrayList();
for(E object:inputlist){
T listItem=(T)method.invoke(object);
returnList.add(listItem);
}
}
return returnList;
}

/**
*
* @param beanClass
* @param name
* @return
* @throws IntrospectionException
* @throws NoSuchMethodException
*/
private static Method findGetter(Class beanClass, String name) throws IntrospectionException, NoSuchMethodException {
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() ){
if (name.equals(propertyDescriptor.getName())) {
return propertyDescriptor.getReadMethod();
}
}
throw new NoSuchMethodException("In class "+beanClass+" no getter method found for field "+name);
}
}

Example:

Consider list of object. and nedd to extract only ids or name from below list:

{ "dataList":
[
{
"name": "Abhishek",
"id": "1"
},
{
"name": "Pooja",
"id": "2"
}
]
}

Now I need list of only names from above list so i will simply

.
.
.

List idlist=ExtractPropertyList(dataList,"id");
.
.
.

This will work for object inside bean class as well.

So happy coding. Stay java addicted.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s