• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterating in multiple ways  RSS feed

 
Dennis Noren
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used a basic ListIterator for traversing through a ListArray, and it works fine for me, producing the same order (or reverse) of how the objects were added to the list. What if I want to traverse the list in some other order, based on attributes of the objects? In particular, these attributes were not known when originally added, or have changed since then.

I don't have a code example because I haven't started this part of my program yet -- just thinking ahead. I know that I can "export" using toArray, and then sort the array, and then build a new ListArray. I'm thinking there must be a more elegant way to do it. I could imagine that it is possible to override next(), hasNext(), etc., but that sounds ambitious. This will be a dynamic situation where I will have to do this operation many times.

I realize this question may be a little difficult to answer without a more concrete example. My application has to do with traversals through a nondirected graph. So I guess a secondary question is to ask about packages especially written for building, accessing and maintaining graph data structures.

Any help is appreciated.
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The list you traverse doesn't contain the objects themselves - it contains references to the objects. It "indexes" your data - it isn't the "data" itself.

Which is to say: you can organize your data into as many different lists - using whatever Java container is most appropriate - as you wish.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at Comparator and Comparable which provide what is called 'natural' ordering. Though I dont think anything supports ordering changing after you add to the collection. But you can use a different Comparator to give you the different ordering.

In other words the objects dont change, nor do their properties, but your ordering style can.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natural ordering in Java is, by definition, the ordering provided by implementing Comparable. Comparators are by definition unnatural.

If you want to change the order over time, you must use Comparators. Every time you want to change the order, use Collections.sort() to re-sort the list using a different Comparator. (You can't use a TreeSet for this sort of thing, as they're not built to allow for a changing order.)

Alternately, if there are say three different orders which you might want to use, then you might just keep three different TreeSets, each one sorted with a different Comparator. To change orders, you just change which TreeSet you look at (without having to re-sort it.) That takes a bit more memory, but might be quicker and more "elegant".
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Natural ordering in Java is, by definition, the ordering provided by implementing Comparable. Comparators are by definition unnatural.

If you want to change the order over time, you must use Comparators. Every time you want to change the order, use Collections.sort() to re-sort the list using a different Comparator. (You can't use a TreeSet for this sort of thing, as they're not built to allow for a changing order.)



TreeSet has a constructor which takes a Comparator. So it is possible, no?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not possible to change the order of iteration after the TreeSet has been created, no. But as I said, it's possible to use several different TreeSets, each of which has its own Comparator.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!