数组集没有 Java 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11934075/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:00:16  来源:igfitidea点击:

No Java implementations for arrayset

javaset

提问by user1595291

How come Java provides several different implementations of the Settype, including HashSetand TreeSetand notArraySet?

Java为何提供该Set类型的几种不同实现,包括HashSetandTreeSetnotArraySet

回答by Jon Skeet

A set based solely on an array of elements in no particular order would always have O(n) time for a containment check. It wouldn't be terribly useful, IMO. When would you wantto use that instead of HashSetor TreeSet?

仅基于没有特定顺序的元素数组的集合将始终有 O(n) 时间进行包含检查。IMO,它不会非常有用。你什么时候想用它代替HashSetor TreeSet

The most useful aspect of an array is that you can get to an element with a particular indexextremely quickly. That's not terribly relevant when it comes to sets.

数组最有用的方面是您可以非常快速地找到具有特定索引的元素。当涉及到集合时,这并不是很重要。

回答by Peter Lawrey

There is CopyOnWriteArraySetwhich is a set backed by an array.

CopyOnWriteArraySet它是一组由数组支持。

This is not particularly useful as its performance is not great for large collections.

这不是特别有用,因为它的性能对于大型集合来说不是很好。

回答by ejboy

Android has android.util.ArraySet(introduced in API level 23) and android.util.ArrayMap(introduced in API level 19).

Android 有android.util.ArraySet(在 API 级别 23 中引入)和android.util.ArrayMap(在 API 级别 19 中引入)。

回答by AlexR

Actually the concrete implementation of Set does not make any sense. Any set stores elements and guaranties their uniqueness. I cannot be sure but it sounds that you want Set implementation that preserves order of elements. If I am right use LinkedHashSet.

其实Set的具体实现没有任何意义。任何集合都存储元素并保证它们的唯一性。我不能确定,但​​听起来您希望 Set 实现保留元素的顺序。如果我是正确的使用LinkedHashSet.

回答by John Smith

Java provides multiple implementations of its CollectionInterfaces that allow for best performance. ArrayListperforms good on many Listoperations.

Java 提供了其Collection接口的多种实现,以实现最佳性能。ArrayList在许多List操作中表现良好。

For SetOperations, which allways require uniquness different implementations offer better performance. If implemented using an array, any modification operation would have to run through all the array elements to check if it is allready in the Set. HashSet and TreeSet simplyfy this check greatly.

对于Set始终需要唯一性的操作,不同的实现可提供更好的性能。如果使用数组实现,任何修改操作都必须遍历所有数组元素以检查它是否已在 Set 中。HashSet 和 TreeSet 极大地简化了这种检查。

回答by Bohemian

The Setinterface has no get-by-index method, such as List.get(int), so there's no use suggesting Set can have array like properties.

Set接口没有 get-by-index 方法,例如List.get(int),因此建议 Set 可以具有类似属性的数组是没有用的。

Ultimately, all "grouping" classes use arrays under the hood to store their elements, but that doesn't mean you have to expose methods for accessing the array.

最终,所有“分组”类都在幕后使用数组来存储它们的元素,但这并不意味着您必须公开访问数组的方法。

回答by user439407

You can always implement it yourself....now granted there probably is only one extremely, extremely limited case where it would be useful(and in that case you could use better data structures anyway) and that is where you have a very large set that almost never changes then an array set would take up SLIGHTLY less memory(no extra pointers) and you would have ever so slightly faster enumeration of the whole set... If you keep the array sorted then you can still get O(lg n) search time.

你总是可以自己实现它......现在授予可能只有一个非常非常有限的情况下它会很有用(在这种情况下你可以使用更好的数据结构),那就是你有一个非常大的集合几乎永远不会改变,那么数组集将占用更少的内存(没有额外的指针),并且您可以稍微快一点地枚举整个集合...如果您保持数组排序,那么您仍然可以获得 O(lg n ) 搜索时间。

However those differences are purely academic. In the real world you would never really want such a beast

然而,这些差异纯粹是学术上的。在现实世界中,您永远不会真正想要这样的野兽

回答by Vitaly Sazanovich

Consider indexed-tree-map, you will be able to access elements by index and get index of elements while keeping the sort order. Duplicates can be put into arrays as values under the same key.

考虑indexed-tree-map,您将能够按索引访问元素并在保持排序顺序的同时获取元素的索引。重复项可以作为相同键下的值放入数组中。