(newbie) converting a hashmap to a treemap

M

Miguel Angel

Hi everyone,

I have to convert a HashMap to a TreeMap in order to print its
contents in a particular sorted way. The trouble is I don't know how
to do it. Can anyone let me know pleeeease. I have been looking at
text books and the Web but so far haven't found it. I would appreciate
your help very much.

Thanking you
MA
 
S

Stefan Waldmann

Miguel said:
Hi everyone,

I have to convert a HashMap to a TreeMap in order to print its
contents in a particular sorted way. The trouble is I don't know how
to do it. Can anyone let me know pleeeease. I have been looking at
text books and the Web but so far haven't found it. I would appreciate
your help very much.

Thanking you
MA

Hi Miguel,

it's not that difficult - create a new TreeMap, put all elements from
your HashMap into the TreeMap, and you're done:

---

HashMap hashMap = new HashMap();

// ...

// convert HashMap to TreeMap
TreeMap treeMap = new TreeMap();
treeMap.putAll(hashMap);
// ready, that's all!

---

By the way, why don't you use a TreeMap from the beginning?


Greetings,
Stefan
 
S

Stefan Waldmann

Hi again,

just found out that there's an even quicker way:

---
HashMap hashMap = new HashMap();

// ...

TreeMap treeMap = new TreeMap(hashMap);
// done!
 
M

Miguel Angel

GREAT! Thank you very much, I knew it would be simple but couldn't
find it anywhere. The reason I need the conversion is that I need
both for different reasons. A HashMap lets me find things quickly. A
TreeMap lets me order them in any way I choose to print them, for
instance, in the order they were used recently (time window). Anyway
thank you once again.
 
M

Miguel Angel

After my original joy, I now have to tell you that your solution
doesn't work. The reason being that a Hashmap belongs to the Map
interface, yet a TreeMap belongs to the collections interface.
My problem is how to turn a HashMap into a collection first so that
then it can be turned into a TreeMap.
Does this explanation make sense?

Thanks
 
C

Chris Smith

Miguel said:
After my original joy, I now have to tell you that your solution
doesn't work. The reason being that a Hashmap belongs to the Map
interface, yet a TreeMap belongs to the collections interface.
My problem is how to turn a HashMap into a collection first so that
then it can be turned into a TreeMap.
Does this explanation make sense?

PMFJI; no, it doesn't. TreeMap implements the Map interface, just like
HashMap does. Perhaps you were mistakenly looking at TreeSet, which
does implement the Collection interface.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Tony Morris

Miguel Angel said:
After my original joy, I now have to tell you that your solution
doesn't work. The reason being that a Hashmap belongs to the Map
interface, yet a TreeMap belongs to the collections interface.
My problem is how to turn a HashMap into a collection first so that
then it can be turned into a TreeMap.
Does this explanation make sense?

Thanks

Classes do not "belong to" interfaces. They may "implement" interfaces.
The java.util.TreeMap class implements the java.util.Map interface, and so
does the java.util.HashMap class.
the code:
new java.util.TreeMap(myHashMap);
will "work" (i.e. compile).

The term "doesn't work" is extremely broad and cannot be diagnosed with a
solution without further context.
Do you mean it doesn't compile ? If you do, then you are wrong.
Do you mean something else ? If so, what ?

I call this the "I have an error, why ?" syndrome.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
M

Michael Borgwardt

Miguel said:
After my original joy, I now have to tell you that your solution
doesn't work. The reason being that a Hashmap belongs to the Map
interface, yet a TreeMap belongs to the collections interface.
My problem is how to turn a HashMap into a collection first so that
then it can be turned into a TreeMap.

Map is a subinterface of Collection. The problem you imagine does not exist.
 
M

Michael Borgwardt

Miguel said:
GREAT! Thank you very much, I knew it would be simple but couldn't
find it anywhere. The reason I need the conversion is that I need
both for different reasons. A HashMap lets me find things quickly. A
TreeMap lets me order them in any way I choose to print them, for
instance, in the order they were used recently (time window).

You are mistaken if you think that TreeMap does not let you
"find things quickly". Depending on the nature of the comparison
and the hash function, TreeMap may in fact be *faster* than
HashMap, and converting the HashMap to a TreeMap almost certainly
takes more time than you saved by using HashMap, even if it is
quicker in your case.

I suggest you use TreeMap throughout.
 
C

Christian Kaufhold

I have to convert a HashMap to a TreeMap in order to print its
contents in a particular sorted way. The trouble is I don't know how
to do it. Can anyone let me know pleeeease. I have been looking at
text books and the Web but so far haven't found it. I would appreciate
your help very much.



new TreeMap(hashMap, particularOrder);




Christian
 

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,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top