Open In App

Initialize an ArrayList in Java

ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

Below are the various methods to initialize an ArrayList in Java:



Initialization with add()

  1. Syntax:
ArrayList<Type> str = new ArrayList<Type>();
       str.add("Geeks");
       str.add("for");
       str.add("Geeks");
  1. Examples: 




// Java code to illustrate initialization
// of ArrayList using add() method
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // create a ArrayList String type
        ArrayList<String> gfg = new ArrayList<String>();
 
        // Initialize an ArrayList with add()
        gfg.add("Geeks");
        gfg.add("for");
        gfg.add("Geeks");
 
        // print ArrayList
        System.out.println("ArrayList : " + gfg);
    }
}

Output:
ArrayList : [Geeks, for, Geeks]
  1. Examples: Using shorthand version of this method 




// Java code to illustrate initialization
// of ArrayList using add() method
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // create a ArrayList String type
        // and Initialize an ArrayList with add()
        ArrayList<String> gfg = new ArrayList<String>() {
            {
                add("Geeks");
                add("for");
                add("Geeks");
            }
        };
 
        // print ArrayList
        System.out.println("ArrayList : " + gfg);
    }
}

Output:

ArrayList : [Geeks, for, Geeks]

Initialization using asList()

  1. Syntax:
ArrayList<Type> obj = new ArrayList<Type>(
      Arrays.asList(Obj A, Obj B, Obj C, ....so on));
  1. Examples: 




// Java code to illustrate initialization
// of ArrayList using asList method
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // create a ArrayList String type
        // and Initialize an ArrayList with asList()
        ArrayList<String> gfg = new ArrayList<String>(
            Arrays.asList("Geeks",
                          "for",
                          "Geeks"));
 
        // print ArrayList
        System.out.println("ArrayList : " + gfg);
    }
}

Output:
ArrayList : [Geeks, for, Geeks]

Initialization using List.of() method

  1. Syntax:
List<Type> obj = new ArrayList<>(
        List.of(Obj A, Obj B, Obj C, ....so on));
  1. Examples: 




// Java code to illustrate initialization
// of ArrayList using List.of() method
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // create a ArrayList String type
        // and Initialize an ArrayList with List.of()
        List<String> gfg = new ArrayList<>(
            List.of("Geeks",
                    "for",
                    "Geeks"));
 
        // print ArrayList
        System.out.println("ArrayList : " + gfg);
    }
}

Output:
ArrayList : [Geeks, for, Geeks]

Initialization using another Collection

  1. Syntax:
List gfg = new ArrayList(collection);
  1. Examples: 




// Java code to illustrate initialization
// of ArrayList using another collection
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // create another collection
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(4);
        arr.add(5);
 
        // create a ArrayList Integer type
        // and Initialize an ArrayList with arr
        List<Integer> gfg = new ArrayList<Integer>(arr);
 
        // print ArrayList
        System.out.println("ArrayList : " + gfg);
    }
}

Output:
ArrayList : [1, 2, 3, 4, 5]

Initialization using  stream() and collect() methods

       1. Syntax:

ArrayList<Type> listName = Stream.of(element1, element2, ..., elementN).collect(Collectors.toCollection(ArrayList::new));

       1. Examples: 




import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GFG {
    public static void main(String args[])
    {
 
        // create a stream of elements using Stream.of()
        // method collect the stream elements into an
        // ArrayList using the collect() method and
        // Collectors.toCollection() method
        ArrayList<String> list
            = Stream.of("Geeks", "For", "Geeks")
                  .collect(Collectors.toCollection(
                      ArrayList::new));
 
        System.out.println(list); // print the ArrayList
    }
}

Output
[Geeks, For, Geeks]

Article Tags :