java 如何将基本类型存储在 Hashmap 或列表中作为值而不是包装类对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27501448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I store primitive types in Hashmap or list as a value instead of a wrapper class object
提问by Mallikarjuna Sangisetty
The question had been asked to me in an interview.
这个问题是在一次采访中被问到的。
I know that primitive types will be converted into wrapper class object to store in any data structure.
我知道原始类型将转换为包装类对象以存储在任何数据结构中。
But interviewer asked me I dont want it to be a wrapper class object and it should be stored as primitive type.
但是面试官问我不希望它是一个包装类对象,它应该存储为原始类型。
How can we do that?
我们怎样才能做到这一点?
回答by Flo
Using Java's Collection API, you cannot do it in a sensible way. Of course you could implement the List
or Map
interfaces yourself and decide to store primitives instead of objects but that would cause you a headache anyway. Java's collection interfaces are all based on objects (Generics don't even play a role in that), so you cannot have an add
or remove
method that takes an int as its argument.
使用 Java 的 Collection API,你不能以一种明智的方式做到这一点。当然,您可以自己实现List
orMap
接口并决定存储原语而不是对象,但这无论如何都会让您头疼。Java 的集合接口都是基于对象的(泛型在这方面甚至没有发挥作用),所以你不能有一个以 int 作为参数的add
orremove
方法。
Let's say you have your own implementation of List<Integer>
that stores int
instead of the Integer
defined by the interface, you could write something like this:
假设您有自己的List<Integer>
存储实现int
而不是Integer
接口定义的实现,您可以编写如下内容:
List<Integer> intList = new MyPrimitiveImplementation<>();
intList.add(42);
Now what happens is that the primitive int 42 gets autoboxed to an Integer
object because the Collection
interface defines the add method as add(Integer e)
. What your implementation could then do would be unboxing the Integer
object again just to get the primitive back.
现在发生的事情是原始 int 42 被自动装箱为一个Integer
对象,因为Collection
接口将 add 方法定义为add(Integer e)
. 然后你的实现可以做的是Integer
再次拆箱对象只是为了取回原语。
So, there's really no point. You either get serious performance trouble (imagine the above autoboxing happening a couple million times), or you lose compatibility with the Collections API. Both are undesirable.
所以,真的没有意义。您要么遇到严重的性能问题(想象一下上述自动装箱发生了几百万次),要么失去了与 Collections API 的兼容性。两者都是不可取的。
回答by Paul Boddington
You can implement the interfaces List
, Set
and Map
however you like. Therefore it would be perfectly possible to write an implementation of List<Integer>
, for example, where all the items are stored internally as primitive int
values. However, if you use the most common implementations of these interfaces (ArrayList
, HashSet
and HashMap
) all of the values will be stored internally as Objects
(which include boxed primitives like Integer
as well as array types like int[]
.
你可以实现的接口List
,Set
并且Map
,只要你喜欢。因此,完全有可能编写 的实现List<Integer>
,例如,其中所有项目都在内部存储为原始int
值。但是,如果您使用这些接口(ArrayList
,HashSet
和HashMap
)的最常见实现,则所有值都将在内部存储为Objects
(包括盒装原语Integer
,如int[]
.
回答by agamesh
You cannot store primitive types in any java.util collection. To do what you want maybe the easiest way is to do a composition like this:
您不能在任何 java.util 集合中存储原始类型。做你想做的也许最简单的方法是做这样的组合:
import java.util.ArrayList;
public class IntList {
ArrayList<Integer> myList;
public void add(int i){
myList.add(new Integer(i));
}
// rest of methods...
}
回答by Sezin Karli
You can put it in an array of the primitive type.
你可以把它放在一个原始类型的数组中。
For an int value,
对于 int 值,
List<int[]> list = new ArrayList<int[]>()
Then put it with
然后把它放在
list.add(new int[]{value});
回答by Guillermo Merino
Java containers store Objects, not primitives, period. You can check the Collections interface documentation, and see that it's based on generics:
Java 容器存储对象,而不是原语。您可以查看 Collections 接口文档,并查看它是基于泛型的:
java.util Interface Collection
Type Parameters: E - the type of elements in this collection All
Superinterfaces: Iterable All Known Subinterfaces: BeanContext, BeanContextServices, BlockingDeque, BlockingQueue, Deque, List, NavigableSet, Queue, Set, SortedSet, TransferQueue
java.util 接口集合
类型参数: E - 此集合中元素的类型 All
超级接口:可迭代所有已知子接口:BeanContext、BeanContextServices、BlockingDeque、BlockingQueue、Deque、 List、NavigableSet、Queue、Set、SortedSet、TransferQueue
And by definition, generics are replaced by objects, not primitives. from wikipedia:
根据定义,泛型被对象取代,而不是基元。来自维基百科:
Note that you cannot use primitive types, ex:
请注意,您不能使用原始类型,例如:
Entry<int, int> pair; // this fails. You have to use Integer instead.
So, concluding, you can write your own list or map implementation, but never implement any of the existing generic interfaces that are based on Collection.
因此,总而言之,您可以编写自己的列表或映射实现,但永远不要实现任何基于 Collection 的现有通用接口。