HashSet的VS ArrayList的速度?插入VS查找(JAVA) [英] HashSet vs ArrayList Speed? Insert vs Lookup ( Java )

查看:134
本文介绍了HashSet的VS ArrayList的速度?插入VS查找(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看着这个问题它让我好奇的是,它使用,VS的Hashset ArrayList中。 HashSet中似乎有一个更好的查找和ArrayList有一个更好的接插件(适用于很多很多的对象)。所以我在我的问题是,既然我不能插入使用一个ArrayList,然后使用HashSet的,我将不得不选择一个或另一个通过它进行搜索。将与一个ArrayList插入,转换到HashSet做查询,会比只插入一个HashSet的那么慢查找整体?或者只是坚持使用ArrayList中,虽然查找更糟糕的是,插入弥补了它?

Looking at this question it made me curious as to which to use, Hashset vs ArrayList. The Hashset seems to have a better lookup and ArrayList has a better insert (for many many objects). So I my question is, since I can't insert using an ArrayList, then search through it using a HashSet, I'm going to have to pick one or the other. Would inserting with an ArrayList, converting to a HashSet to do the lookups, be SLOWER overall than to just insert into a HashSet then lookup? Or just stick with the ArrayList, although the lookup is worse, the inserting makes up for it?

推荐答案

这很大程度上取决于集合的大小和你使用它的方式。即你可以重复使用相同的 HashSet的复制,这将节省您的时间。或者,你可以把它们保持最新。

It very much depends on the size of the collection and the way you use it. I.e. you can reuse the same HashSet for copying and this would save you time. Or you can keep them up-to-date.

为每个元素查找一个 HashSet的拷贝将永远是慢。

Creating a HashSet copy for each element lookup will always be slower.

您还可以利用 LinkedHashSet 具有快速插入和 HashSet的的查找速度为代价稍差内存消耗和 O(N) 指数(INT)操作。

You can also utilize LinkedHashSet which has quick insertion and a HashSet's look up speed at cost of a little worse memory consumption and O(N) index(int) operation.

这篇关于HashSet的VS ArrayList的速度?插入VS查找(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆