java 从 Arrays.asList 返回的列表是否与原始数组集合保持相同的顺序?

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

Does the list returned from Arrays.asList maintain the same order as the original array collection?

javacollectionsarraylist

提问by

I've got an ArrayList that I'm iterating over several times, and it looks like it isn't maintaining the order of iteration. I looked deeper, and it appears that the custom iterator tag that was written for this iteration (by someone else) is first taking the passed in ArrayList and using Arrays.asList to bridge it to an Object[] collection before iterating. Is the order of iteration being lost? Is that expected with Arrays.asList?

我有一个 ArrayList 正在迭代几次,看起来它没有保持迭代顺序。我看得更深入,似乎(由其他人)为此迭代编写的自定义迭代器标记首先采用传入的 ArrayList 并在迭代之前使用 Arrays.asList 将其桥接到 Object[] 集合。迭代顺序是否丢失?Arrays.asList 是预期的吗?

EDIT:

编辑:

Here is the what the operation does with the original collection passed into the iterator tag:

以下是该操作对传递给迭代器标签的原始集合所做的操作:

if(collection.getClass().isArray()) {
    iterator = Arrays.asList((Object[]) collection).iterator();
} else if(collection instanceof Collection) {
    iterator = ((Collection) collection).iterator();
} else if(collection instanceof Iterator) {
    iterator = (Iterator) collection;
} else if(collection instanceof Map) {
    iterator = ((Map) collection).entrySet().iterator();
}

采纳答案by Powerlord

The List is still backed by the original array, so items should appear in the same order as they do in the array.

List 仍然由原始数组支持,因此项目应该以与它们在数组中相同的顺序出现。

Also, note that asList is a generic method, so Arrays.asList(T[])will return a List<T>.

另外,请注意 asList 是一个通用方法,因此Arrays.asList(T[])将返回一个List<T>.

As for the other direction, List's toArray

至于其他的方向,ListtoArray

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

以适当的顺序(从第一个元素到最后一个元素)返回一个包含此列表中所有元素的数组。

There are two toArray methods. The first returns an Object[], the second take an array as its argument and returns as array of the same type.

有两种 toArray 方法。第一个返回 an Object[],第二个将数组作为其参数并作为相同类型的数组返回。

Something like this is not uncommon, using an array of String as an example:

像这样的事情并不少见,以字符串数组为例:

String[] y = x.toArray(new String[0]);

(This example is taken from the JavaDocfor List<E>)

(此示例取自JavaDocfor List<E>

回答by Zach L

Possibly where the iteration is being thrown off is if collectionis a Map

可能抛出迭代的地方是 ifcollection是 Map

 else if(collection instanceof Map) {
    iterator = ((Map) collection).entrySet().iterator();
}

If collectionis specifically a HashMap, the order of the collection's elements are indeed tossed out the window.

如果collection特别是HashMap,则集合元素的顺序确实被抛出了窗口。

If collectionis a TreeMap, it shouldmaintain the order of the collection, based on sorting. However, if you make a TreeMap out of an array of Comparableobjects, you shouldn'texpect the array and the TreeMap to have the same order, since the TreeMap will sort the contents of the array.

如果collectionTreeMap,它应该根据排序维护集合的顺序。但是,如果从Comparable对象的数组中创建 TreeMap,则不应期望数组和 TreeMap 具有相同的顺序,因为 TreeMap 将对数组的内容进行排序。

And you could also be getting some other Collectionwhich will muck around with order like HashSet, PriorityQueue, etc.

而且你也可能会得到一些其他的东西Collection,它们会像HashSetPriorityQueue等一样乱七八糟

However, if collectionis never a Map and always an Array, Arrays.asListshouldmaintain the order. My best guess is that your issues lies either outside of your code snippet, or in one of the cases besides where you passed an Array.

但是,如果collection永远不是 Map 而始终是 Array,Arrays.asList则应保持顺序。我最好的猜测是你的问题要么在你的代码片段之外,要么在你传递数组之外的情况之一。

回答by Peter Lawrey

You can make this more generic replacing Collectionwith Iterable.

您可以Collection使用Iterable.

} else if(collection instanceof Iterable) {
    iterator = ((Iterable) collection).iterator()

Note: primitive arrays like int[].class.isArray() == true, but they cannot be cast with Object[]

注意:原始数组像int[].class.isArray() == true,但不能用Object[]