Create List containing n Copies of Specified Object Example in Java
Description
The following code shows how to create List containing n Copies of Specified Object Example.
Example
/* w w w. j av a 2 s . co m*/
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List list = Collections.nCopies(5, "A");
Iterator itr = list.iterator();
while (itr.hasNext())
System.out.println(itr.next());
}
}
The code above generates the following result.