TreeMap question

L

laredotornado

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
 
L

Lew

Patricia said:
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.
 
M

Mike Schilling

Lew said:
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.
 
L

Lew

Mike said:
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.
 
M

Mike Schilling

Lew said:
But not an inherent feature of Java class evolution.


'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.
 
L

Lew

Mike said:
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?
 
M

Mike Schilling

Lew said:
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.
 
A

Arne Vajhøj

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
 
Joined
Dec 24, 2010
Messages
19
Reaction score
0
laredotornado said:
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


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
 

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,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top