Create List containing n Copies of Specified Object - Java Collection Framework

Java examples for Collection Framework:Collections Utility Methods

Description

Create List containing n Copies of Specified Object

Demo Code

 
import java.util.Collections;
import java.util.List;
import java.util.Iterator;
 
public class Main {
  public static void main(String[] args) {
    List list = Collections.nCopies(5,"A");
   //from  ww  w.j av  a  2s . co m
    System.out.println("List contains, ");
    Iterator itr = list.iterator();
   
    while(itr.hasNext())
      System.out.println(itr.next());
   
  }
}

Result


Related Tutorials