public static < T > List < T > repeatEachToSize ( final Collection < T > c , final int size ) { N . checkArgNotNegative ( size , "size" ) ; checkArgument ( size == 0 || notNullOrEmpty ( c ) , "Collection can not be empty or null when size > 0" ) ; if ( size == 0 || isNullOrEmpty ( c ) ) { return new ArrayList < T > ( ) ; } final int n = size / c . size ( ) ; int mod = size % c . size ( ) ; final List < T > result = new ArrayList <> ( size ) ; for ( T e : c ) { for ( int i = 0 , len = mod -- > 0 ? n + 1 : n ; i < len ; i ++ ) { result . add ( e ) ; } if ( result . size ( ) == size ) { break ; } } return result ; }
Repeats the elements in the specified Collection one by one till reach the specified size .