Collections.nCopies() in Java Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report 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 Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer: Comprehensive Course Expert Guidance for Efficient Learning Hands-on Experience with Real-world Projects Proven Track Record with 100,000+ Successful Geeks Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 28 Feb, 2018 Like Article Save Article Previous How to Make a Collection Read-Only in Java? Next Print all triplets in sorted array that form AP Share your thoughts in the comments Add Your Comment Please Login to comment...