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.

5 Years
Discussion Span
Last Post by Taywin

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.


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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.