Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created December 24, 2021 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravkukade/e9323411ee8d6d9c694bbda9653510c6 to your computer and use it in GitHub Desktop.
Save gauravkukade/e9323411ee8d6d9c694bbda9653510c6 to your computer and use it in GitHub Desktop.
A Java program to add all elements of the specified collection to the end of the ArrayList. Read more at https://coderolls.com/arraylist-addall-method-in-java/
import java.util.ArrayList;
import java.util.List;
/**
* A Java program to add all elements of the specified collection
* to the end of the ArrayList
*
* @author Gaurav Kukade at coderolls.com
*/
public class ArrayListAddAllExample {
public static void main(String[] args) {
// ArrayList contains actresses
List<String> actresses1 = new ArrayList<String>();
actresses1.add("Natalie Portman");
actresses1.add("Abigail Anderson");
actresses1.add("Jennifer Lawrence");
System.out.println("Actresses collection 1: \n"+ actresses1 );
// ArrayList contains actresses
List<String> actresses2 = new ArrayList<String>();
actresses2.add("Angelina Jolie");
actresses2.add("Scarlett Johansson");
System.out.println("Actresses collection 2: \n"+ actresses2);
// Add actresses2 collections all elements to actresses1 collection using addAll()method
actresses1.addAll(actresses2);
System.out.println("Actresses : \n"+ actresses1);
}
}
Actresses collection 1:
[Natalie Portman, Abigail Anderson, Jennifer Lawrence]
Actresses collection 2:
[Angelina Jolie, Scarlett Johansson]
Actresses :
[Natalie Portman, Abigail Anderson, Jennifer Lawrence, Angelina Jolie, Scarlett Johansson]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment