开发者

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

开发者 https://www.devze.com 2023-02-10 13:12 出处:网络
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 th开发者_开发知识库at the custom iterator

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 th开发者_开发知识库at 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?

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();
}


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

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

As for the other direction, List's toArray

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.

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 JavaDoc for List<E>)


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

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

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

If collection is a TreeMap, it should maintain the order of the collection, based on sorting. However, if you make a TreeMap out of an array of Comparable objects, you shouldn't expect the array and the TreeMap to have the same order, since the TreeMap will sort the contents of the array.

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

However, if collection is never a Map and always an Array, Arrays.asList should maintain 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.


You can make this more generic replacing Collection with 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[]

0

精彩评论

暂无评论...
验证码 换一张
取 消