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.
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.
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 Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.