Working with polymorphism collections, list, arraylists etc.

I have a sorted collection that I would like to split based on one of the objects properties. All records > 0 to one; all less than to another. I'm trying to think of the best way to implement this. I'm thinking Comparable/Comparator sorting and then iterating through the collection until I hit 0 and splitting there but it also occurs to me that theres gotta be an easier way.

Recommended Answers

Can you iterate through the collection, test each object and put it in one of the new lists based on its value?

Jump to Post

All 5 Replies

Can you iterate through the collection, test each object and put it in one of the new lists based on its value?

That was the other option; I'm guessing that's about the only way to do it. I was actually thinking about a HashMap as well tho. Two keys with multiple values based on the < > 0.

A lot depends on the application's requirements.

instead of hashmap, you can also use ArrayList...but it depends on your requirements..

If you are using an array already, you could do a sort and get the index in the middle for splitting as well. The algorithm has bigger Big O and a bit more complex but requires less space use in the process.