
Signature
Eric Sosman
esosman@acm-dot-org.invalid
> Try just putting the (key,value) pairs in an ordinary HashMap.
> When you want to visit all the values (or all the pairs) in order
> by value, use .values() to extract the values (or .entrySet() to
> extract the pairs), sort them, and traverse the sorted data instead
> of the original Map.
you mean to say that i should get the values and put them in an
arraylist and then access the values from the arraylist instead of
getting them from the map ?
> That might not be suitable for all situations, but works for
> many. If it's not the right answer for your predicament, you'll
> need to describe your purposes more fully.
what you suggested should i work. it would have been good if i were
able to directly put the values in some collection which does a sorting
of values.
i want to take a list of vendors from the database alongwith their ids
and store them in a collection. then i want to keep adding and removing
values from this collection based on some user inputs.
Thanks
shikha
Shikha - 16 Aug 2006 14:25 GMT
> > Try just putting the (key,value) pairs in an ordinary HashMap.
> > When you want to visit all the values (or all the pairs) in order
[quoted text clipped - 5 lines]
> arraylist and then access the values from the arraylist instead of
> getting them from the map ?
Eric, I was about to try this out but i realised that this wouldnt
work. Because i need to populate the values from a Map. the reason
being that when the user selects a particular value i need to pick the
corresponding key for the value. so if i populate the values from
arraylist i wouldnt be able to get the corresponding id.
any other suggestions ?
thanks
shikha
Eric Sosman - 16 Aug 2006 16:06 GMT
Shikha wrote On 08/16/06 09:25,:
>>> Try just putting the (key,value) pairs in an ordinary HashMap.
>>>When you want to visit all the values (or all the pairs) in order
[quoted text clipped - 11 lines]
> corresponding key for the value. so if i populate the values from
> arraylist i wouldnt be able to get the corresponding id.
If you need the (key,value) pairs sorted by value, not
just the values themselves, use theMap.entrySet() to get
them: it gives you a Collection of Map.Entry objects, each
representing one (key,value) pair. Sort that Collection of
pairs using a Comparator that looks at the values.

Signature
Eric.Sosman@sun.com
Stefan Ram - 16 Aug 2006 16:16 GMT
>If you need the (key,value) pairs sorted by value, not
>just the values themselves, use theMap.entrySet() to get
>them: it gives you a Collection of Map.Entry objects, each
>representing one (key,value) pair. Sort that Collection of
>pairs using a Comparator that looks at the values.
When a value is inserted, it could be inserted into both
collections, keeping their respective invariants: So the
sorted collection will stay sorted (insertation sort).
Just inserting a single value into the correct position
should be faster than to sort anew. Shifting all following
values could be faster if a linked list is used.
However: All this is very low-level. So it might be best to
encapsulate the whole decision and implement it in the most
convenient way at first. Then, when the first copies of the
program are sold and it turns out that this implementation
really is too slow, it still can be optimized for version 1.1.