| Author |
Store several values in a one key in Java 1.4
|
Eun Mi
Greenhorn
Joined: Oct 10, 2011
Posts: 6
|
|
Hello,
I usually program in Java 1.5 or higher, but now I need a HashMap, where the key is a String a and value - a list of values. Does Java 1.4 have something similar?
|
 |
Sumit Patil
Ranch Hand
Joined: May 25, 2009
Posts: 296
|
|
|
Java 1.4 HashMap
|
Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
You cannot store multiple values on the same key in a Map. If you add the second value, it overwrites the first value.Remember you may have to cast the elements retrieved from a Collection or Map in Java1.4, otherwise they are all of type java.lang.Object.
If you want to store multiple values in a Map, you can’t; … but you can use a List as a value. The technique is the same in Java1.4 as it is in Java7, only Java1.4 didn’t support generics.
If you look at the documentation for HashMap, it will probably say “since 1.2”, so you can tell which versions of Java support it. Every class ought to have a since field.
|
 |
Vinod Tiwari
Ranch Hand
Joined: Feb 06, 2008
Posts: 458
|
|
You can create a custom class which suits your requirements of multiple list of values. Then you can use a MAP and add it in value part for the key. However since JDK 1.4 does not supports generic you may have to take care of runtime surprises
|
Vinod Tiwari | Twitter
|
 |
Eun Mi
Greenhorn
Joined: Oct 10, 2011
Posts: 6
|
|
|
Thank you, Vinod Tiwari! I just have never programmed in 1.4. I'll try it !
|
 |
 |
|
|
subject: Store several values in a one key in Java 1.4
|
|
|