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
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.