Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > TreeMap problem...

Reply
Thread Tools

TreeMap problem...

 
 
Harry
Guest
Posts: n/a
 
      09-22-2003
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


 
Reply With Quote
 
 
 
 
Boris Stumm
Guest
Posts: n/a
 
      09-22-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)-kl.de, http://wwwhis.informatik.uni-kl.de/staff/Stumm/
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      09-22-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Harry
Guest
Posts: n/a
 
      09-23-2003
Spot on mate, didn't notice that - will follow your advice - cheers!


"John C. Bollinger" <(E-Mail Removed)> wrote in message
news:bkndg5$l0h$(E-Mail Removed)...
> 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
> (E-Mail Removed)
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TreeMap - getting values out in the order they went in? news.amnet.net.au Java 2 04-22-2004 08:12 AM
Empty values in TreeMap - cannnot be removed? news.amnet.net.au Java 3 04-19-2004 08:57 PM
(newbie) converting a hashmap to a treemap Miguel Angel Java 9 04-15-2004 01:28 PM
Help with sorting values in a TreeMap Wendy S Java 4 02-19-2004 07:17 AM
HashMap vs TreeMap Ahmed Moustafa Java 2 08-10-2003 03:31 AM



Advertisments