How to add multiple items to the ArrayList


There are various ways to initialize an ArrayList with multiple elements and add elements to it.

1. using add() method repeatedly :

ArrayList<String> str=new ArrayList<>();
str.add("banana");
str.add("Apple");
System.out.println(str);

2. Using ArrayList.asList() method :

ArrayList<String> list=new ArrayList<>(Arrays.asList("America","Europe","Asia"));
System.out.println(list);

3.Using Collections.addAll() :

ArrayList<String> new_list =new ArrayList<>();
Collections.addAll(new_list, "Car","Bike");
System.out.println(new_list);

4. Using Java Streams :

ArrayList<String> another_list=Stream.of("Lenovo","Dell","Apple").collect(Collectors.toCollection(ArrayList::new));
System.out.println(another_list)

Outputs:
[banana, Apple]
[America, Europe, Asia]
[Car, Bike]
[Lenovo, Dell, Apple]