TreeMap problem...

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

  1. Harry

    Harry Guest

    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
    #1
    1. Advertising

  2. Harry

    Boris Stumm Guest

    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
    #2
    1. Advertising

  3. 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
     
    John C. Bollinger, Sep 22, 2003
    #3
  4. Harry

    Harry Guest

    Spot on mate, didn't notice that - will follow your advice - cheers!


    "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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa

    HashMap vs TreeMap

    Ahmed Moustafa, Aug 9, 2003, in forum: Java
    Replies:
    2
    Views:
    46,453
    Roedy Green
    Aug 10, 2003
  2. Wendy S
    Replies:
    4
    Views:
    14,245
    Jim McMaster
    Feb 19, 2004
  3. Miguel Angel
    Replies:
    9
    Views:
    14,048
    Christian Kaufhold
    Apr 15, 2004
  4. tomek milewski
    Replies:
    4
    Views:
    1,195
    Ingo R. Homann
    Jul 17, 2006
  5. Jeff Higgins

    TreeMap/Comparator a mapping problem

    Jeff Higgins, May 20, 2007, in forum: Java
    Replies:
    3
    Views:
    811
    Jeff Higgins
    May 20, 2007
Loading...

Share This Page