Join two ArrayLists in Java
Given two ArrayLists in Java, the task is to join these ArrayLists.
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.
Input: ArrayList1: [Geeks, For, ForGeeks], ArrayList2: [GeeksForGeeks, A computer portal]
Output: ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal]
Input: ArrayList1: [G, e, e, k, s], ArrayList2: [F, o, r, G, e, e, k, s]
Output: ArrayList: [G, e, e, k, s, F, o, r, G, e, e, k, s]
Approach: ArrayLists can be joined in Java with the help of Collection.addAll() method. This method is called by the destination ArrayList and the other ArrayList is passed as the parameter to this method. This method appends the second ArrayList to the end of the first ArrayList.
Below is the implementation of the above approach: