tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Collections > NCopies

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.

File Name  :  
com/bethecoder/tutorials/utils/collections/NCopies.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
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(46);
    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]



 
  


  
bl  br