TreeMap question

Discussion in 'Java' started by laredotornado, Dec 22, 2010.

  1. Hi,

    I'm using Java 1.6. I have a TreeMap that maps Integer to objects.
    Given an integer that is given to be a key in this map, how can I
    figure out what the key is immediately before the given one? Let me
    know if this question doesn't make sense, - Dave
    laredotornado, Dec 22, 2010
    #1
    1. Advertising

  2. laredotornado

    Lew Guest

    laredotornado wrote:
    >> I'm using Java 1.6. I have a TreeMap that maps Integer to objects.
    >> Given an integer that is given to be a key in this map, how can I
    >> figure out what the key is immediately before the given one? Let me
    >> know if this question doesn't make sense, - Dave


    Patricia Shanahan wrote:

    > Use your TreeMap's lowerKey(K) method.
    >
    > More generally, use the API documentation for Java 1.6. If you use an
    > older version of the documentation, you will miss out on some handy
    > java.util features.


    Corollary point: This is a use case for a more specific type declaration than
    'Map'.

    The rule to use the most general applicable type does yield a very specific
    type when that's called for.

    A rigorous type-based approach would induce a declaration of 'NavigableMap' to
    use 'lowerKey()'.

    (Generics elided.)

    Interesting that so much effort went into the collections framework for 6.
    You are one of the few who really noticed. It's largely unheralded, being not
    nearly so alluring as functors and fooferol.

    This is Collections 3G - the third generation. They sure come in useful for
    workaday programming. Maybe that's why they work on this area so hard.

    --
    Lew
    Ceci n'est pas une pipe.
    Lew, Dec 23, 2010
    #2
    1. Advertising

  3. "Lew" <> wrote in message
    news:ieu9mf$pmt$...
    > laredotornado wrote:
    >>> I'm using Java 1.6. I have a TreeMap that maps Integer to objects.
    >>> Given an integer that is given to be a key in this map, how can I
    >>> figure out what the key is immediately before the given one? Let me
    >>> know if this question doesn't make sense, - Dave

    >
    > Patricia Shanahan wrote:
    >
    >> Use your TreeMap's lowerKey(K) method.
    >>
    >> More generally, use the API documentation for Java 1.6. If you use an
    >> older version of the documentation, you will miss out on some handy
    >> java.util features.

    >
    > Corollary point: This is a use case for a more specific type declaration
    > than 'Map'.


    And demonstrates an interesting feature of Java class evolution.
    lowerKey(K) is in all existing concrete implementations of SortedMap, and
    (it seems to me) would be useful and straightforward to implement in any
    reasonable implementation. But because it's not possible to add a method to
    an interface while keeping compatibility, lowerKey(K) was not added to
    SortedMap itself.
    Mike Schilling, Dec 23, 2010
    #3
  4. laredotornado

    Lew Guest

    Mike Schilling wrote:
    > And demonstrates an interesting feature of Java class evolution. lowerKey(K)


    But not an inherent feature of Java class evolution.

    > is in all existing concrete implementations of SortedMap, and (it seems to me)
    > would be useful and straightforward to implement in any reasonable
    > implementation. But because it's not possible to add a method to an interface
    > while keeping compatibility, lowerKey(K) was not added to SortedMap itself.


    'java.sql.ResultSet' did not receive such a delicate treatment. They changed
    that interface for 6.

    --
    Lew
    Ceci n'est pas une pipe.
    Lew, Dec 23, 2010
    #4
  5. "Lew" <> wrote in message
    news:ieuhl8$4aj$...
    > Mike Schilling wrote:
    >> And demonstrates an interesting feature of Java class evolution.
    >> lowerKey(K)

    >
    > But not an inherent feature of Java class evolution.
    >
    >> is in all existing concrete implementations of SortedMap, and (it seems
    >> to me)
    >> would be useful and straightforward to implement in any reasonable
    >> implementation. But because it's not possible to add a method to an
    >> interface
    >> while keeping compatibility, lowerKey(K) was not added to SortedMap
    >> itself.

    >
    > 'java.sql.ResultSet' did not receive such a delicate treatment. They
    > changed that interface for 6.


    JDBC seems to be exempt from the usual compatibility requirements. I think
    it's because JDBC drivers are expected to be upgraded as the interfaces
    change.
    Mike Schilling, Dec 23, 2010
    #5
  6. laredotornado

    Lew Guest

    Lew wrote:
    >> 'java.sql.ResultSet' did not receive such a delicate treatment.  They
    >> changed that interface for 6.

    >


    Mike Schilling wrote:
    > JDBC seems to be exempt from the usual compatibility requirements.  I think
    > it's because JDBC drivers are expected to be upgraded as the interfaces
    > change
    >


    It carries the same risks as for any other interface. I was on a
    project a while back that used a database library from source, and the
    transition to Java 6 broke the implementation of the 'ResultSet'
    interface because the shop hadn't upgraded that source with the Java
    version change.

    Mind you, had that project been smart enough to use a JAR for that
    library - well, they weren't. Nevertheless, if you are correct about
    the reason it's an unbelievable stupid reason. Why should drivers
    have to change with the Java version? They should only have to change
    if the database engine changes. If the Powers That Be had not changed
    the interface, it would not have broken all those JDBC drivers. Why
    did they cause all that unnecessary work?

    --
    Lew
    Lew, Dec 23, 2010
    #6
  7. "Lew" <> wrote in message
    news:...
    > Lew wrote:
    >>> 'java.sql.ResultSet' did not receive such a delicate treatment. They
    >>> changed that interface for 6.

    >>

    >
    > Mike Schilling wrote:
    >> JDBC seems to be exempt from the usual compatibility requirements. I
    >> think
    >> it's because JDBC drivers are expected to be upgraded as the interfaces
    >> change
    >>

    >
    > It carries the same risks as for any other interface. I was on a
    > project a while back that used a database library from source, and the
    > transition to Java 6 broke the implementation of the 'ResultSet'
    > interface because the shop hadn't upgraded that source with the Java
    > version change.


    Yeah, we used to have code that couldn't be compiled under Java 6 because
    the JDBC drivers didn't implement the new version of the interfaces. (That
    is, the ANT script knew enough to compile it under 1.5 instead.)

    >
    > Mind you, had that project been smart enough to use a JAR for that
    > library - well, they weren't. Nevertheless, if you are correct about
    > the reason it's an unbelievable stupid reason.


    I might not be correct. I'd love for someone with more knowledge to step in
    here.
    Mike Schilling, Dec 24, 2010
    #7
  8. On 23-12-2010 02:23, Mike Schilling wrote:
    > "Lew" <> wrote in message
    > news:ieuhl8$4aj$...
    >> Mike Schilling wrote:
    >>> is in all existing concrete implementations of SortedMap, and (it
    >>> seems to me)
    >>> would be useful and straightforward to implement in any reasonable
    >>> implementation. But because it's not possible to add a method to an
    >>> interface
    >>> while keeping compatibility, lowerKey(K) was not added to SortedMap
    >>> itself.

    >>
    >> 'java.sql.ResultSet' did not receive such a delicate treatment. They
    >> changed that interface for 6.

    >
    > JDBC seems to be exempt from the usual compatibility requirements. I
    > think it's because JDBC drivers are expected to be upgraded as the
    > interfaces change.


    Oracle comes with a JDBC driver per Java SE version.

    That is one way of handling it.

    Another is to build it with an old JDK.

    But no matter what it requires special handling.

    Arne
    Arne Vajhøj, Dec 24, 2010
    #8
  9. laredotornado

    code learner

    Joined:
    Dec 24, 2010
    Messages:
    19

    Hi laredotornado,

    as i have understand your requirement i guess the following code may help you

    public static void main(String[] args) {
    TreeMap<Integer,String> map = new TreeMap<Integer,String>();
    map.put(1, "one");
    map.put(3, "three");
    map.put(5, "five");
    map.put(7, "seven");
    Integer key = map.floorKey(4);
    System.out.println("key = " + key);

    }

    bye
    learner
    code learner, Dec 26, 2010
    #9
    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,411
    Roedy Green
    Aug 10, 2003
  2. Harry

    TreeMap problem...

    Harry, Sep 22, 2003, in forum: Java
    Replies:
    3
    Views:
    469
    Harry
    Sep 23, 2003
  3. Wendy S
    Replies:
    4
    Views:
    14,228
    Jim McMaster
    Feb 19, 2004
  4. Miguel Angel
    Replies:
    9
    Views:
    13,820
    Christian Kaufhold
    Apr 15, 2004
  5. Darko Aleksic

    TreeMap and Comparator question

    Darko Aleksic, Jun 20, 2006, in forum: Java
    Replies:
    4
    Views:
    10,392
    Darko Aleksic
    Jun 20, 2006
Loading...

Share This Page