Conversion between a Java collection and an array
@author Ixenos
Array-to-set arrays.aslist (T ... a)
The first conclusion : Using Arrays.aslist (T ... a) to convert an array to a collection
(T...A) is a variable parameter, as (t[] a) can be
aslist (T ... a)
Returns a fixed-size list supported by the specified array, which is a pure array in the form of a list, just a view of the original array (changes to the returned list are "directly written" to the array.) )
The returned list is serializable and implements theRandomAccess
This method also provides a convenient way to create a fixed-length list (which is a list) that is initialized to contain multiple elements:
List<string> stooges = arrays.aslist ("Larry", "Moe", "Curly");
Why is it a fixed size? Not "ArrayList"?
Because this is not the "ArrayList" you imagined, and there is no commitment to ArrayList, just return an interface type list!
SOURCE Analysis:
First look at the Aslist method:
1 Public static <T> list<t> aslist (T ... a) {2 returnnew Arraylist<>(a); 3 }
It was suddenly discovered that the Aslist method originally called the constructor of the private static inner class ArrayList of the arrays class (rather than the ArrayList in the collection frame):
0 private Final e[] A;
1ArrayList (e[] array) {2 a = objects.requirenonnull (array); 3 }
(Note: This class also inherits the Abstractlist<e> interface, similar to the ArrayList structure of the tool class (all maintaining an array), but the internal principle is different, it is just a list view of the array. Below is the source code)
1 Private Static classArraylist<e>extendsAbstractlist<e>2 Implementsrandomaccess, Java.io.Serializable3 {4 Private Static Final LongSerialversionuid = -2764017481108945198l;5 Private Finale[] A;6 7 ArrayList (e[] array) {8A =objects.requirenonnull (array);9 }Ten One @Override A Public intsize () { - returna.length; - } the - @Override - Publicobject[] ToArray () { - returnA.clone (); + } - + @Override A@SuppressWarnings ("Unchecked") at Public<T>t[] ToArray (t[] a) { - intSize =size (); - if(A.length <size) - returnArrays.copyof ( This. A, size, -(CLASS<?extendsT[]>) A.getclass ()); -System.arraycopy ( This. A, 0, a, 0, size); in if(A.length >size) -A[size] =NULL; to returnA; + } - the @Override * PublicE Get (intindex) { $ returnA[index];Panax Notoginseng } - the @Override + PublicE Set (intindex, E Element) { AE OldValue =A[index]; theA[index] =element; + returnOldValue; - } $ $ @Override - Public intindexOf (Object o) { -E[] A = This. A; the if(O = =NULL) { - for(inti = 0; i < a.length; i++)Wuyi if(A[i] = =NULL) the returni; -}Else { Wu for(inti = 0; i < a.length; i++) - if(O.equals (A[i])) About returni; $ } - return-1; - } - A @Override + Public Booleancontains (Object o) { the returnIndexOf (o)! =-1; - } $ the @Override the PublicSpliterator<e>Spliterator () { the returnSpliterators.spliterator (A, spliterator.ordered); the } - in @Override the Public voidForEach (consumer<?SuperE>action) { the Objects.requirenonnull (action); About for(E e:a) { the action.accept (e); the } the } + - @Override the Public voidReplaceAll (unaryoperator<e>operator) {Bayi objects.requirenonnull (operator); theE[] A = This. A; the for(inti = 0; i < a.length; i++) { -A[i] =operator.apply (A[i]); - } the } the the @Override the Public voidSort (comparator<?SuperE>c) { - Arrays.sort (A, c); the } the}arrays Internal class ArrayList source code
From this inner class to see the returned list as an array, you can not add and delete elements, can only be replaced and traversed without altering the structure of the operation
The Requirenonnull method in the constructor method is a non-null check (from the Objects Tool Class):
1 Public static <T> t requirenonnull (t obj) {2 ifnull)3 thrownew nullpointerexception (); 4 return obj; 5 }
Requirenonnull
Visible, did not do any processing, just actually passed a generic array to reference A, indicating that the internal data structure is an array
The method of ToArray array interface is directly used Collection<e> interface ToArray () and ToArray (t[] a):
1.object[] ToArray ()
Returns an array that contains all the elements in this collection, but is of type Object , and an unwarranted downward transformation is illegal, so even type conversions are not possible;
If collection makes certain assurances about the order of elements returned by its iterators (such as the comparator parameter), then this method must return the elements in the same order;
The returned array will be "safe," because this collection does not maintain any references to the returned array, which is not the view of the array, and the caller can arbitrarily modify the returned array
In a word , generally do not use this good, type is flawed ...
2.<t> t[] ToArray (t[] a)
The run- time type of the returned array is the same as the run-time type of the specified array, so it is possible to pass in a specified type array;
if the specified array can hold the collection, an array containing this collection element is returned, otherwise a run-time type with the specified array and a new array of this collection size will be assigned ;
If the specified array can hold collection, and there is space left (that is, the elements of the array are more than the elements of collection), then the elements in the array immediately trailing collection are set to null ( only if the caller knows that this collection does not contain any null elements
To determine the length of the collection with this method. )
This method allows precise control of the run-time type of the output array and, in some cases, can be used to save allocation overhead;
In a word , an array of formal parameters is a container, which is sufficient (null), not enough to build a new one (the same as the run-time type and the formal parameter)
3. Conclusion : Using the method ToArray (t[] a) to convert the set into an array
Parameter (t[] a) to specify the run-time type
Conversion between a Java collection and an array