• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Store several values in a one key in Java 1.4

 
Eun Mi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.4 HashMap
 
Campbell Ritchie
Sheriff
Pie
Posts: 50180
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 466
1
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Eun Mi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Vinod Tiwari! I just have never programmed in 1.4. I'll try it !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic