大数组“int”类型需要传递给通用数组 &收藏品
我正在从函数生成一个包含 int
类型元素的大型数组(大小>1000)。我需要将此数组传递给泛型类型数组,但由于泛型类型数组不接受原始类型数组,因此我无法这样做。
我害怕使用 Integer
类型数组,因为在创建、性能、使用空间(12 字节对象的数组)方面这样做的成本很高。大尺寸数组。当我需要对数组元素执行一些加法操作时,它会创建不可变的 Integer
。
最好的搭配是什么?
编辑只是为了消除一些混乱,我需要将 int[] 传递给签名类型的方法:void setKeys(K...keys)
。
I am generating a large arrays(size>1000) with elements of int
type, from a function. I need to pass this array to a generic type array but since the generic type array doesnt accept arrays of primitive type, I am unable to do so.
I fear to use the Integer
type array since it will be costly, in terms of creation, performance, space used(an array of 12 byte objects) when doing so for a large size arrays. More it will create immutable Integer
s when I need to perform some addition operations on the array elements.
What would be the best way to go with ?
EDIT Just to remove some confusions around, I need to pass int[] to a method of signature type: void setKeys(K... keys)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我假设您的意思是
int[]
应该是一组键...而不仅仅是一个键。那是不可能的。泛型类型的类型参数必须是引用类型。您的用例要求
K
为int
。您有两种选择:
顺便说一句,
Integer
类为较小的int
值保留了Integer
对象的缓存。如果您使用 Integer.valueOf(int) 创建对象,那么您很有可能会获得对预先存在的对象的引用。 (当然,这只有效,因为 Integer 对象是不可变的。)I assume that you mean that
int[]
should be the set of keys ... not just one key.That is impossible. The type parameters of a generic type have to be reference types. Your use-case requires
K
to be aint
.You have two choices:
Integer
(or a mutableint
holder class) and pay the performance penalty, orIncidentally, the
Integer
class keeps a cache ofInteger
objects for smallint
values. If you create your objects usingInteger.valueOf(int)
there's a good chance that you will get a reference to an pre-existing object. (Of course, this only works becauseInteger
objects are immutable.)如果您的数组大约有 1000 个(甚至 10,000 或 100,000 个)元素,则内存和性能方面的成本差异可能不会明显,除非您每个数组都处理数千次。用 Integer 编写代码,如果出现性能问题,稍后进行优化。
If your arrays are on the order of 1000 (or even 10,000 or 100,000) elements, the cost difference in terms of memory and performance probably wouldn't be noticeable unless you're processing the arrays thousands of times each. Write the code with Integer and optimize later if you have performance problems.
如果您关心性能,您可以编写一个包装公共
int
的简单类,这意味着您可以进行调用并仍然根据需要改变它。话虽如此,我确实同意您在执行此操作之前要绝对确定需要这种性能改进。If you're that concerned about performance, you could write a simple class that wraps a public
int
, thus meaning you can make your call and still mutate it as needed. Having said that, I do agree that you want to make absolute sure you need this performance improvement before doing it.如果您确实需要担心装箱/拆箱整数的性能影响,您可以考虑GNU Trove,特别是他们的的功能。也就是说,我不确定您是否需要这个,也不确定这是否正是您正在寻找的。
TIntArrayList
。它可以让您在基元的支持下模拟 ArrayListIf you actually do need to worry about the performance implications of boxing/unboxing integers, you could consider GNU Trove, specifically their
TIntArrayList
. It lets you mimic the functionality of anArrayList<Integer>
while being backed by primitives. That said, I'm not certain you need this, and I'm not certain this is exactly what you are looking for.如果您不希望整数永久装箱,可以传入 Google Collections 库 (http://guava-libraries.googlecode.com/ svn/tags/release08/javadoc/com/google/common/primitives/Ints.html#asList(int...)),这将是一个
List
支持数组。这些值在被访问时会被装箱,因此只有当这些值没有被多次访问时这才有意义。If you don't want the integers permanently boxed, you could pass in the result of
Ints.asList()
from the Google Collections library (http://guava-libraries.googlecode.com/svn/tags/release08/javadoc/com/google/common/primitives/Ints.html#asList(int...)), which would be aList<Integer>
backed by the array. The values will get boxed as they're accessed, so this only makes sense if the values are not being accessed lots of times.