java – Speedup HashSet和HashMap性能
发布时间:2020-12-15 05:20:34 所属栏目:Java 来源:网络整理
导读:在 Java中,我有: SetInteger set = new HashSetInteger();callVoidMethod(set);...public static void callVoidMethod(SetInteger set) { SetInteger superset = new HashSetInteger(set); ... // I just added this loop to show that I'm adding quite a
在
Java中,我有:
Set<Integer> set = new HashSet<Integer>(); callVoidMethod(set); ... public static void callVoidMethod(Set<Integer> set) { Set<Integer> superset = new HashSet<Integer>(set); ... // I just added this loop to show that I'm adding quite a lot // well,it depends on conditions,sometimes I add nothing,// but it is unpredictable and do not know if add something for (int i = 0; i < 1000; i++) { ... if (conditionSatisfied) superset.add(someValue); ... } } 上面的代码是简化的,想法是通过引用将集合传递给void方法并创建集合的完整副本,以便我们能够向副本添加一些新元素(这里是超集)并且不要触摸设置为我们在退出void方法时不需要它. 我的代码适用于大量的数据处理,如果没有更快的方法来制作副本,那么我想优化HashSet本身,例如我不需要Integers作为键,而是更好的原始int.在MyHashSet中实现一个int []数组是个好主意吗? 如果可能,我会有兴趣使用相同的想法来改善这个: Map<Integer,ArrayList<Item>> map = new HashMap<Integer,ArrayList<Item>>(); 编辑:我只需要速度性能优化.我不需要漂亮的可维护代码和内存. 解决方法
你以后如何处理这些物体?如果您只是进行查找或类似的事情,将它们分开并检查两者可能会更快,而不是制作完整副本.所以,
public static void callVoidMethod(Set<Integer> set) { Set<Integer> superset = new HashSet<Integer>(); ... if (conditionSatisfied) superset.add(someValue); ... if(set.contains(value) || superset.contains(value)) doSomething(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |