In this article, we will discuss how to create ArrayList using Collections class’s utility nCopies() method
This is used to create ArrayList containing multiple copies of same elements or say same objects
1. Creating ArrayList using nCopies method :
Method signature:
public static List nCopies(int n, Object object);
Where,
- n – number of copies to be created
- object – element value (or object for which multiple copies required)
CreateArrayListUsingNCopies.java
package in.bench.resources.java.collections;
import java.util.ArrayList;
import java.util.Collections;
public class CreateArrayListUsingNCopies {
public static void main(String[] args) {
// 7 copies of String object
ArrayList<String> lstString = new ArrayList<String>(
Collections.nCopies(7, "BRN"));
// printing List of String object
System.out.println("ArrayList of String objects : "
+ lstString);
// create a customer object
Customer cust = new Customer(101, "Berry");
// 3 copies of Customer object - user-defined object
ArrayList<Customer> lstCustomer = new ArrayList<Customer>(
Collections.nCopies(3, cust));
// printing List of Customer object
System.out.println("\n\nArrayList of Customer objects : "
+ lstCustomer);
}
}
class Customer {
// member variables
int custId;
String custName;
// 2-arg constructors
public Customer(int custId, String custName) {
super();
this.custId = custId;
this.custName = custName;
}
// overriding toString method
@Override
public String toString() {
return "\nCustomer ["
+ "Id=" + custId
+ ", Name=" + custName
+ "]";
}
}
Output:
ArrayList of String objects : [BRN, BRN, BRN, BRN, BRN, BRN, BRN]
ArrayList of Customer objects : [
Customer [Id=101, Name=Berry],
Customer [Id=101, Name=Berry],
Customer [Id=101, Name=Berry]]
Related Articles:
- Creating ArrayList using nCopies method of Collections class
- How to get size or length of an ArrayList
- Adding element to ArrayList at specified index position
- Remove element from ArrayList at specified index position
- How to delete a element and delete all elements of an ArrayList
- How to get sublist from ArrayList
- How to check whether particular element is present in ArrayList
- Comparing two ArrayList objects using containsAll() method
- Adding one ArrayList to another ArrayList using addAll() method
- Replacing ArrayList element with new value using set() method
- How to get maximum element from ArrayList
- How to get minimum element from ArrayList
- Various ways to iterate through ArrayList
- How to reverse ArrayList contents
- Sorting ArrayList in descending order
- Sorting ArrayList using sort() method of List in Java 8
- Sorting list of objects on multiple fields using Comparator in Java
- Remove duplicate elements from ArrayList in Java
- How to remove duplicate elements of ArrayList maintaining insertion order
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/List.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
Happy Coding !!
Happy Learning !!