I have a List that contains objects like an adress - e.g. city, street, name.
And always need receive 3 lists: first ordered by city, second ordered by street, third ordered by name. It is clear that possible order consequent开发者_JS百科ially the same list. But it consumes many time. Is it possible to create something like Iterator that can depend from parameter return all members of collection in corresponding order? Or exists another solution?
Thanks.
Not with the standard java collection framework. What you are asking to work, you have to build different search trees that operate on same collection.
It is like having indices on different columns in DB
You can build 3 Comparators- 1 based on each criteria item, and then call Collections.sort(List, Comparator) to get each sorting.
If you're doing this numerous times on the same list, make 3 copies.
Note that the iterator idea wouldn't really be terribly efficient as it would need some way to trace through the list that would be at least as ugly as doing the sort. That is, the iterator would need to know which item comes next, in some way other than the natural order of the list. It's just as easy to re-sort the list as try to navigate it out of natural order.
精彩评论