Java Collections fill specified object to List

Description

Java Collections fill specified object to List


import java.util.Collections;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<String> list = Collections.nCopies(5, "CSS");

    System.out.println(list);//w w w .j a  va  2 s .  co  m
    
    
    //the list is read only list
    list.add("demo 2s.com");
    
    System.out.println(list);
  }
}



PreviousNext

Related