NCopies
The following example shows how to use Collections.nCopies API.
It returns an immutable and serializable list consisting of n copies of the specified object.
package com.bethecoder.tutorials.utils.collections;
import java.util.Collections;
import java.util.List;
public class NCopies {
/**
* @param args
*/
public static void main ( String [] args ) {
//4 copies of "NCopies" as list
List<String> strList = Collections.nCopies ( 4 , "NCopies" ) ;
System.out.println ( strList ) ;
//6 copies of Boolean.TRUE as list
List<Boolean> boolList = Collections.nCopies ( 6 , Boolean.TRUE ) ;
System.out.println ( boolList ) ;
//6 copies of 'A' as list
List<Character> charList = Collections.nCopies ( 6 , 'A' ) ;
System.out.println ( charList ) ;
//4 copies of '6' as list
List<Integer> intList = Collections.nCopies ( 4 , 6 ) ;
System.out.println ( intList ) ;
}
}
It gives the following output,
[NCopies, NCopies, NCopies, NCopies]
[true, true, true, true, true, true]
[A, A, A, A, A, A]
[6, 6, 6, 6]