How to Add All Items From a Collection to an ArrayList in Java?
Given a Collection with some values, the task is to add all the items of this 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.
ArrayList<T> list =
List<Integer> collection1 = Arrays.asList(
"ArrayList with all "
"elements of collection "
+ collection1 +
List<String> collection2 = Arrays.asList(
"ArrayList with all"
" elements of collection "
+ collection2 +
ArrayList with all elements of collection [1, 2, 3]: [1, 2, 3]
ArrayList with all elements of collection [GFG, Geeks, GeeksForGeeks]: [GFG, Geeks, GeeksForGeeks]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!