How to store primitive types in Hashmap or list as value instead of wrapper class object
The question was asked to me in an interview.
I know primitive types will be converted to wrapper class object for storage in any data structure.
But the interviewer asked me that I don't want it to be an object of the wrapper class and it should be stored as a primitive type.
How can we do this?
source to share
Using the Java Collection API, you cannot do it in a sane way. Of course, you could implement interfaces List
or Map
yourself and choose to store primitives instead of objects, but that would still give you a headache. All Java collection interfaces are object-based (Generics don't even play a role in that), so you can't use a method add
or remove
that takes int as an argument.
Let's say you have your own implementation List<Integer>
that stores int
instead of the Integer
one given by the interface, you can write something like this:
List<Integer> intList = new MyPrimitiveImplementation<>();
intList.add(42);
Now what happens is that the primitive int 42 gets autoboxed to the object Integer
because the interface Collection
defines the add method as add(Integer e)
. Then your implementation could unpack the object again Integer
to put the primitive back.
So it doesn't really make sense. You either get serious performance issues (imagine the aforementioned autoboxing happens a couple of million times) or you lose compatibility with the collections API. Both are undesirable.
source to share
You can implement interfaces List
, Set
and Map
however you like. So it would be perfectly possible to write an implementation List<Integer>
, for example, where all the elements are stored internally as primitive values int
. However, if you use the most common implementations of these interfaces ( ArrayList
, HashSet
and HashMap
), all values will be stored internally Objects
(including the boxed type primitives as Integer
well as an array of type int[]
.
source to share
Java stores store objects, not primitives, period. You can check the Collections interface documentation and see that it is generics based:
The java.util interface collection
Type Parameters: E - the type of items in this collection All
Superinterfaces: Iterable all known subinterfaces: BeanContext, BeanContextServices, BlockingDeque, BlockingQueue, Deque, List , NavigableSet, Queue, Set , SortedSet, TransferQueue
And by definition, generics are replaced with objects, not primitives. from wikipedia :
Please note that you cannot use primitive types like:
Entry<int, int> pair; // this fails. You have to use Integer instead.
So, in conclusion, you can write your own list or map implementation, but never use any of the existing generic collection-based interfaces.
source to share
You cannot store primitive types in any java.util collection. To do what you want, perhaps the easiest way is to do a composition like this:
import java.util.ArrayList;
public class IntList {
ArrayList<Integer> myList;
public void add(int i){
myList.add(new Integer(i));
}
// rest of methods...
}
source to share