I’m trying to create a Hashtable as in the following:
Hashtable<int, ArrayList<byte>> block = new Hashtable<int, ArrayList<byte>>();
but I am getting an error on both int and byte saying “Dimensions expected after this token”.
If I use something like:
Hashtable<String, byte[]> – all is good. Can someone explain why?
Thanks.
In Java’s core collection classes you can only store reference types (something that extends a java.lang.Object). You cannot store primitives like
intandbyte. Note that an array likebyte[]is no primitive but also a reference type.As @Giuseppe mentioned, you can define it like this:
and then put primitive
int‘s in it as keys:because since Java 1.5, autoboxing will automatically change the primitive
intinto anInteger(a wrapper) behind the scenes.If you need more speed (and measured the wrapper collection classes are the problem!) you could use a 3rd party library that can store primitives in their collections. An example of such libraries are Trove and Colt.