Wednesday, September 8, 2010

How to get List of property values from List of objects?

package com.javautils.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import com.javautils.beans.Address;
import com.javautils.beans.User;


public class CollectionUtil
{
    public static void main(String[] args)
    {
        User user1 = new User(1,"AAAA", "Hyd");
        User user2 = new User(2,"BBBB", "Bnglr");
        User user3 = new User(3,"CCCC", "Chennai");
        User user4 = new User(4,"DDD", "Kochin");
        User user5 = new User(5,"AAAA", "US");
        Collection<User> users =new ArrayList<User>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        users.add(user5);

        Collection<String> userNames = CollectionUtil.getPropertyValuesAsCollection(users, "name", String.class, false);
        for (String userName : userNames)
        {
            System.out.println(userName);
        }
        
        Collection<Integer> ids = CollectionUtil.getPropertyValuesAsCollection(users, "id", Integer.class, false);
        for (Integer id : ids)
        {
            System.out.println(id);
        }

        Collection<address>
addresses = CollectionUtil.getPropertyValuesAsCollection(users, "address", Address.class, false);
        for (Address address : addresses)
        {
            System.out.println(address);
        }
    }


    public static <T>  Collection<T> getPropertyValuesAsCollection(Collection collection, String propertyName, Class<T> propertyType)
    {
        return getPropertyValuesAsCollection(collection, propertyName, propertyType, true);
    }

    public static <T>  Collection<T> getPropertyValuesAsCollection(Collection collection,String propertyName,Class<T> propertyType,boolean allowDuplicates)
    {
        if(CollectionUtil.isEmpty(collection))
        {
            return null;
        }
        Collection<T> valueCollection = null;
        if(allowDuplicates)
        {
            valueCollection = new ArrayList<T>();
        }
        else
        {
            valueCollection = new HashSet<T>();
        }
        return getPropertyValuesAsCollection(collection, propertyName, propertyType, valueCollection);
    }

    private static <T>  Collection<T> getPropertyValuesAsCollection(Collection collection, String propertyName, Class<T> propertyType, Collection<T> valueCollection)
    {
        for (Object obj : collection)
        {
            try
            {
                T object = (T) ReflectionUtil.getFieldValue(obj, propertyName);    
                valueCollection.add(object);
            }
            catch (NoSuchFieldException e)
            {
                e.printStackTrace();
            }catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
        }
        return valueCollection;
    }

    public static boolean isEmpty(Collection collection)
    {
        return (collection == null || collection.size() == 0);
    }
}

package com.javautils.beans;
public class Address
{
    private String address;
    public Address()
    {
    }    
    public Address(String address)
    {
        this.address = address;
    }    
    public String toString()
    {
        return "Address [address=" + address + "]";
    }
    public String getAddress()
    {
        return address;
    }
    public void setAddress(String address)
    {
        this.address = address;
    }
    
}

package com.javautils.beans;
public class User
{
    private int id;
    private String name;
    private Address address;
    
    public User()
    {
    }
    public User(int id, String name, String address)
    {
        this.id = id;
        this.name = name;
        this.address = new Address(address);
    }    
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public Address getAddress()
    {
        return address;
    }
    public void setAddress(Address address)
    {
        this.address = address;
    }
}

No comments:

Post a Comment