Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Although I'd refactor out that middle chunk of the iteration.)
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 75650
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic