Why java.util.Map value can be primitive array but not single primitive
Asked Answered
C

2

5

Here are two examples

Map value as Single value

private Map<Short, Boolean> _Booleans = new HashMap<Short, Boolean>(); //works
private Map<Short, boolean> _Booleans = new HashMap<Short, boolean>(); //not allowed

Map value as Array

private Map<Short, Boolean[]> _Booleans = new HashMap<Short, Boolean[]>(); //works
private Map<Short, boolean[]> _Booleans = new HashMap<Short, boolean[]>(); //works!

Primitive wrappers are forced on single value, but primitive arrays are allowed, why is that?

Sub question: Is it possible to use single value primitives with a Map?

Castleman answered 12/8, 2015 at 12:29 Comment(0)
B
6

Maps can only store Objects. Primitives are not Objects unless they are in a wrapper class (Boolean instead of boolean in your example).

Arrays are always Objects, regardless of what kind of data they contain. Therefore, they can be stored in a Map without any problems.

In Java, typically you should prefer using primitive values, as they are faster and smaller in regards to memory usage. However, there are some cases (like the one in your example) where the boxed type is more useful. In some cases (typically when using generics), autoboxing might take effect.

An important difference between a primitive and its Object counterpart is that the Object can be null. A primitive is NEVER null.

Berkman answered 12/8, 2015 at 13:11 Comment(3)
Thanks! +1 Do you know if Map<Short, boolean> booleans = new HashMap<Short, boolean>(); would be somehow be possible in Java, maybe other libs?Castleman
I don't believe it is. Java.Utils.Map uses generics as the key and the value, and generics can only be Objects. Of course, you could write your own implementation of a Map that takes exclusively Short Objects as a key and boolean primitives as the value, but that seems like a lot of work for very little (if any) payoff. Besides, I think Java will autobox the values for you (primitives will get wrapped to Objects, Objects get unwrapped to primitives) using the built in Map, so in this case there's no reason to force the Map to take primitives.Berkman
That's one of the top reasons I don't like about java. They try to force you to use their wrappers everywhere. Regardless of it is actually needed or not. Thanks for the answers!Castleman
A
1

as @Nik meantioned, Map stores only Objects (any class in Java)

Now for your question, why is array of primitive boolean can be stored -> it's because in Java (and many other languages as well) Array is an Object

boolean cc[]={true, false};
System.out.println(cc instanceof Object);//gives true

Note -> that is true, only because cc has an actual array in it, but if you put null to it -> it will not longer an instance of Object so:

cc=null;
System.out.println(cc instanceof Object);//gives false

Note 2 -> for your subquestion: You cannot use them directly Consider this example:

HashMap aMap=new HashMap();
        int x=120;//int value of 120
        aMap.put("120",x);//parsed Integer with value of 120
        x=aMap.get("120");//compiler error - Type mismatch

One more thing, since you are interesting in this subject. I would suggest you a book called "Effective Java", by Joshua Bloch who by the way developed the Collections in Java, and Map is of course a collection.

Aureole answered 12/8, 2015 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.