我有一个不会改变内存密集的ArrayList,它只存储两个字段,
public class ExampleObject{
private String string;
private Integer integer;
public ExampleObject(String stringInbound, Integer integerInbound){
string = stringInbound;
integer = integerInbound;
}
我将使用此对象填充ArrayList
ArrayList<ExampleObject> = new ArrayList<ExampleObject>();
对于原始的硬核性能,使用hashset要好得多吗?如果我的ArrayList增长到数量不等的大量项目,那么我会注意到对象的ArrayList和hashset之间有很大的差异吗?
答案 0 :(得分:3)
虽然它们都是Collection,但我建议您阅读Set和List之间的差异。
它们不用于同一目的。因此,在考虑性能之前,请选择符合实施要求的那个。
答案 1 :(得分:1)
这完全取决于你在做什么。数据是如何添加的?怎么访问?它被删除的频率是多少?
例如,在某些情况下,拥有String[]
和int[]
的并行数组可能更好 - 您可以避免集合类开销以及int
到{{1}的装箱}}。根据你正在做的事情,这可能真的很棒或非常愚蠢。
随着数据集变大,内存消耗会对性能产生很大影响。几年前,几位IBM研究人员在Building Memory-efficient Java Applications上做了一个简洁的演示,每个关注性能的人都应该阅读。