TreeMap problem...

H

Harry

My treemap contains the following keys 1, 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2
, 3 , 3 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 4 , 4 - all as String's!

I want be able to go directly to the first key that = 3 & process all
subsesquent 3's without having to start from the beginning & test each value
until 3 is reached etc...

Is this possible? - should I stop trying to be too efficient & use a
different collection perhaps?

thanks

harry
 
B

Boris Stumm

Harry said:
I want be able to go directly to the first key that = 3 & process all
subsesquent 3's without having to start from the beginning & test each value
until 3 is reached etc...

Iterator threes = treemap.submap("3", "4").values().iterator();

Sometimes it helps to read the API Doc.
 
J

John C. Bollinger

Harry said:
My treemap contains the following keys 1, 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2
, 3 , 3 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 4 , 4 - all as String's!

No, it doesn't. No Map contains more than two or more mappings where
the keys are equals() each other. That's part of the point of using a
Map. If you try to insert a new mapping for a key that is equal to an
existing key in the map, then the existing mapping is replaced.
I want be able to go directly to the first key that = 3 & process all
subsesquent 3's without having to start from the beginning & test each value
until 3 is reached etc...

Perhaps what you want to do is to store store your objects in
intermediate collections that are stored in the Map. For instance, all
objects that should be associated with key "3" go into a List, and it is
that List that is associated with "3" in your Map. That not only gets
around your matching key problem, it also solves your lookup problem:
you just look up the desired key, and then process all the objects in
the associated List.
Is this possible? - should I stop trying to be too efficient & use a
different collection perhaps?

It depends heavilly on just what you actually want to accomplish. There
may be a more straightforward way.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top