# TreeMap problem...

Discussion in 'Java' started by Harry, Sep 22, 2003.

1. ### HarryGuest

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

Harry, Sep 22, 2003

2. ### Boris StummGuest

Harry wrote:
> 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.

--
Dipl. Inf. Boris Stumm, AG Heterogene Informationssysteme, FB Informatik.
Kaiserslautern University of Technology, P.O. Box 3049, 67653 Kaiserslautern,
Germany. Tel.: +49 631 205 3264, Fax: +49 631 205 3299, Room: 36/338
-kl.de, http://wwwhis.informatik.uni-kl.de/staff/Stumm/

Boris Stumm, Sep 22, 2003

3. ### John C. BollingerGuest

Harry wrote:

> 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
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

John C. Bollinger, Sep 22, 2003
4. ### HarryGuest

"John C. Bollinger" <> wrote in message
news:bkndg5\$l0h\$...
> Harry wrote:
>
> > 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
>
>

Harry, Sep 23, 2003