Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes How to Group a Collection of Pojo's based on some pojo property? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Group a Collection of Pojo Watch "How to Group a Collection of Pojo New topic
Author

How to Group a Collection of Pojo's based on some pojo property?

Subin kumar
Greenhorn

Joined: Oct 07, 2005
Posts: 3
I have a collection of Pojos's. The Pojo has 3 attributes
in it. How can i group this collection of pojos, based
on one pojo property, and later put the grouped pojo's
into a Map and use the pojo property as the key.

Can we acheive this in a generic way..?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

So you have a Collection<Pojo> and want a Map<PropertyType,List<Pojo>>? I'd probably just create a static helper method that does this conversion for you. One method per property that is.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Subin kumar
Greenhorn

Joined: Oct 07, 2005
Posts: 3
Yes Rob..

I got your point..

But is there any generic way to do it..?


For eg;

I have a two pojo's with me

public class Pojo1 {
private String prop1;
private BigDecimal prop2;

public Pojo1(String prop1, BigDecimal prop2){
this.prop1 = prop1; this.prop2 = prop2;
}
}

class Pojo2 {
private String prop3;
private String prop4;
}

List<Pojo1> l = new ArrayList<Pojo1>();
l.add(new Pojo1("ABC",1.2));
l.add(new Pojo1("BCD",1.2));
l.add(new Pojo1("CDE",1.2));
l.add(new Pojo1("ABC",2.3));
l.add(new Pojo1("BCD",2.3));
l.add(new Pojo1("CDE",2.3));

Now.. as per you suggestion ,

i create a sttaic method , say,

public static Map<String,List<Pojo1>> groupList(String propertyName, List c) {

}

// what i am looking for is , a method to which groups the collection c, based on the
// property name which i pass (here it can be "prop1" or "prop2" )
// and give me Map with the Key as the distinct property name values, and the map value
//for the key as the subset of collection , which has the pojo property value as the
//key

i.e., by calling groupList("prop1", l)


the Map should give me:
KEY - Value List<Pojo>
----------------------------------
"ABC" - [ Pojo1("ABC",1.2), Pojo1("ABC",2.3) ]
"BCD" - [ Pojo1("BCD",1.2), Pojo1("BCD",2.3) ]
"CDE" - [ Pojo1("CDE",1.2), Pojo1("CDE",2.3) ]

Also , how acheive it in a generic way, so that even if i have
group it by "prop2", i can use the same method...

And groupList should work with any List (i.e., List<Pojo1> oe List<Pojo2> )
based on the property name which we are passing to it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Using reflection you could create a Map<Object,List<Pojo>>. You'll need java.lang.reflect.Field to get the values for that one property.
 
jQuery in Action, 2nd edition
 
subject: How to Group a Collection of Pojo's based on some pojo property?