The role of Collections.nCopies() is to return an immutable list which contains n copies of given object. This function helps if we want to create a list with n copies of given object. The newly allocated data object is tiny i.e, it contains a single reference to the data object.
Syntax :
public static <T> List<T> nCopies(int number, T object) where, number is the number of copies of object and object represents the element which will appear number times in the returned list. T represents generic type.
Exception : The function throws IllegalArgumentException if value of number is less than 0.
Example :
Java
// Java code to show implementation // of Collections.nCopies() import java.util.*; class GFG { // Driver code public static void main(String[] args) { // creating a list where first argument // represents the number of copies and // the second argument represents the // element to be copied for 'number' times // This will create 4 copies of the objects. List list = Collections.nCopies(4, "GeeksforGeeks"); // Displaying the list returned System.out.println("The list returned is :"); Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.print(itr.next() + " "); } System.out.println("\n"); List list1 = Collections.nCopies(3, "GeeksQuiz"); // Displaying the list returned System.out.println("The list returned is :"); Iterator itr1 = list1.iterator(); while (itr1.hasNext()) { System.out.print(itr1.next() + " "); } System.out.print("\n"); } } |
Output :
The list returned is : GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks The list returned is : GeeksQuiz GeeksQuiz GeeksQuiz
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
- Java.io.DataOutputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 1
- Java.io.InputStream Class in Java
- Java.io.SequenceInputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 2
- Java.io.Console class in Java
- Java.io.FilterOutputStream Class in Java
- Java.io.StringWriter class in Java
- Java.io.CharArrayReader Class in Java
- Java.io.FileInputStream Class in Java
- Java.io.CharArrayWriter class in Java | Set 1
- Java.io.CharArrayWriter class in Java | Set 2
- Java.io.DataInputStream class in Java | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

