@ SuppressWarnings ( "unchecked" ) public static Object convert ( Collection < ? > collection , Class < ? > arrayType ) { int size = collection . size ( ) ; // Allocate a new Array Object newArray = Array . newInstance ( Optional . ofNullable ( arrayType ) . filter ( Class :: isArray ) . map ( Class :: getComponentType ) . orElse ( ( Class ) Object . class ) , size ) ; Iterator < ? > iterator = collection . iterator ( ) ; // Convert and set each element in the new Array for ( int i = 0 ; i < size ; i ++ ) { Object element = iterator . next ( ) ; try { ReflectUtils . arraySet ( newArray , i , element ) ; } catch ( Throwable e ) { System . err . println ( "set element to array exception, " + e . getMessage ( ) ) ; } } return newArray ; }
Returns an array object this method converts a collection object to an array object through the specified element type of the array .