Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any easy way to split an Arraylist into multiple lists based on a property of the objects?

 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an ArrayList of similar objects.

Now I have to split the lists into multiple lists based on a property of these objects.

Here is the sample data set
List:
A{1, A}
A{2, A}
A{3, B}
A{4, B}
A{5, B}
A{6, C}

Result should be

List 1:
A{1, A}
A{2, A}

List2:
A{3, B}
A{4, B}
A{5, B}

List3:
A{6, C}

Any body has a easier way doing this...
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps commons-collections could help you with such a task.

But I would try to put those all in a map, using that splitting/filtering property as key and a List as value:


After this block, map.values() will return a Collection of List objects containing what you are looking for.
In case the splitter property is of a custom type, make sure that type has a proper equals() and hashCode() implementations.
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can only use Java 1.4

Not sure if your code will work in my case.



 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Java 4, just strip down the generics from my code.
I actually have to split the ArrayList into an array.

You need to split the ArrayList into an array of lists ? If so, map.values().toArray().
 
forums UseR
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Costi Ciudatu wrote:Perhaps commons-collections could help you with such a task.

But I would try to put those all in a map, using that splitting/filtering property as key and a List as value:


Can you please explain whats going on here..., I have not worked much with Maps.., and I have only used Java 1.4
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Although I'd refactor out that middle chunk of the iteration.)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Or, if feeling particularly refactory:)(Dealing with people unfamiliar with refactoring for a job, so aways coming up with examples to show them :)
 
Mahesh Gopal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will split the main list into sub list based on the size of the individual child's list size.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post. Doesn't it look better Always use those tags.

Since Java7 has come and gone since the last post on that thread was posted, I challenge you to use a Stream to split the List.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic