6

I need a map where the values are of different types, like integer, string etc. The problem with Java is that primitives here are not Objects which suggests that it may not be possible to have a hybrid dictionary. I want to confirm this.

bluish
  • 26,356
  • 27
  • 122
  • 180
ada
  • 91
  • 1
  • 2
  • 3
  • see also http://stackoverflow.com/questions/2001755/using-int-as-a-type-parameter-for-java-util-dictionary – Fortega Dec 13 '10 at 10:02
  • 1
    You might want to share more of your design for comments. In my experience using different types of values in a Map often is a sign of less than optimal design. – Christoffer Hammarström Dec 13 '10 at 10:05
  • I want a JSON desrializer that can create a tree, based on just the message instead of converting it into a given target class. So I want a dictionary(dictionary or map whatever the name may be) or a List as output like Jon Skeet says assuming automatic boxing in Java. Also assuming there is a nice JSON serialization-deserialzation library that does this. Gson for example needs a type into which the message will be deserialized into. Hence Gson is not helpful for me. Hope you understand my problem. – ada Dec 13 '10 at 10:25
  • Can you post what your dictionary/map supposed to look like and what the JSON output you exect from it? – gigadot Dec 13 '10 at 12:05

4 Answers4

9

It sounds like you just want a Map<String, Object> (or whatever your key type is).

Primitive values will be boxed appropriately:

Map<String, Object> map = new HashMap<String, Object>();

map.put("int", 20);
map.put("long", 100L);
// etc

Note that in order to retrieve the value and unbox it, you have to mention the specific wrapper type:

// Explicit unboxing
int x = (int) (Integer) map.get("int");
// Implicit unboxing
int y = (Integer) map.get("int");
// USing a method from Number instead
int z = ((Integer) map.get("int")).intValue();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Geez Jon, you really don't sleep. I was about to post the same thing. :) – Buhake Sindi Dec 13 '10 at 09:57
  • Another option for numbers is Map – Peter Lawrey Dec 13 '10 at 10:00
  • 2
    @Peter Lawrey, the OP wants a value also of type String. Number doesn't qualify. – Buhake Sindi Dec 13 '10 at 10:01
  • 1
    @The Elite Gentleman, good point, curiously enough that Map and Dictionary non-generic would have done the job. – Peter Lawrey Dec 13 '10 at 10:24
  • Thanks Jon, I need somethig like that. See my comment above to understand my problem. – ada Dec 13 '10 at 10:27
  • @ada: Looks like you want JSONObject: http://www.json.org/javadoc/org/json/JSONObject.html – Jon Skeet Dec 13 '10 at 10:39
  • Actually I found something better. http://stackoverflow.com/questions/4078360/parsing-json-into-mapstring-entity-with-flexjson FlexJSON has JSONDeserializer> which does the job very nicely. All this time I was trying with Hashtable instead. Thanks for the help Jon for suggesting the interface Map. – ada Dec 13 '10 at 11:57
3

When you put primitives into a Map in Java, they get Auto-Boxed into their object form. For example, if you have a Map defined as:

Map<Integer, String> myMap = new HashMap<Integer, String>();

then you can use primitives of type int, as they will be auto-boxed into an Integer.

As for your original question, defining a Map as such:

// using Integer for key type, can be something else
Map<Integer, Object> myMap = new HashMap<Integer, Object>();

then you should be able to put any Java object in the map.

Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88
1

You could exploit autoboxing and use Integer instead of int and so forth.

The corresponding types (Integer, Double, Bool, ...) inherit object, so you could use a standard Map<Object, Whatever> and throw arbitrary stuff at it.

David
  • 3,787
  • 2
  • 29
  • 43
0

You can use Integer instead of int.

bluish
  • 26,356
  • 27
  • 122
  • 180
munissor
  • 3,755
  • 22
  • 24