Mega Code Archive

 
Categories / Java Tutorial / SWT
 

Multiple-Copy Collections

The nCopies() method of Collections is similar to using fill(), then copy(), with a List: public static List nCopies(int n, Object element) This creates a List with the same element repeated throughout the list. nCopies() method creates an immutable collection. import java.util.Collections; import java.util.LinkedList; import java.util.List; public class MainClass {   public static void main(String[] a) {     List list = new LinkedList(Collections.nCopies(10, null));     System.out.println(list);   } } [null, null, null, null, null, null, null, null, null, null]