• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..?
 
Sheriff
Posts: 22257
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Subin kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22257
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic