How to add all items from a collection to an ArrayList in Java?
Input: Collection = [1, 2, 3]
Output: ArrayList = [1, 2, 3]
Input: Collection = [GFG, Geek, GeeksForGeeks]
Output: ArrayList = [GFG, Geek, GeeksForGeeks]
- Get the Collection whose items are to be added into the ArrayList
- Create an ArrayList
- Add all the items of Collection into this ArrayList using ArrayList.addAll() method
- ArrayList with all the items of Collections have been created.
Below is the implementation of the above approach:
ArrayList with all elements of collection [1, 2, 3]: [1, 2, 3]
ArrayList with all elements of collection [GFG, Geeks, GeeksForGeeks]: [GFG, Geeks, GeeksForGeeks]
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.