commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "matthew.hawthorne" <ma...@apache.org>
Subject Re: [commons-collections]: how to do reverse iteration in TreeBidiMap
Date Wed, 30 Mar 2005 14:32:25 GMT
rinke hoekstra wrote:
> maybe a stupid question, or maybe I am missing something, but I cannot sort out how to

> do a reverse iteration over the values of a TreeBidiMap, and couldn't find much about
it 
> on the web.
> I want to retrieve the 10 greatest values from a HashMap, and thought it best to use
a 
> TreeBidiMap for it.
> 
> I have this:
> 
> TreeBidiMap bidiMap = new TreeBidiMap(myHashMap);
> OrderedBidiMap invMap = bidiMap.inverseOrderedBidiMap(); //creates inversed map
> OrderedMapIterator oit = invMap.orderedMapIterator();
> while (oit.hasPrevious()) {
>       Float value = (Float)oit.previous();
>       .....
> }
> 
> 
> The iterator however seems to be initialized at the first element, so hasPrevious() is

> usually false, and the loop isn't run. 
> 
> How to initialize the iterator at the last element, so you can start iterating using

> hasPrevious()? 

How about something like this?

TreeBidiMap bidiMap = new TreeBidiMap(myHashMap);
Collection values = bidiMap.values()
values.sort()
values.reverse()
for(Iterator i=values.iterator(); i.hasNext();) {
	Float value = (Float)i.next()
}

I wouldn't worry about looping using hasPrevious(), I'd just reverse the
collection so that it is in descending order.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message