How do you create a list in Java?

A List of elements can be created using multiple ways.

Way #1

Create a List without specifying the type of elements it can holds. Compiler will throw warning message for it.

List list = new ArrayList();

Create a List and specify the type of elements it can holds.

Way #2

List<CustomObject> list = new ArrayList<>();

Way #3

Create and initialize the list in one line.

List<CustomObject> list = Arrays.asList(object1, object 2);


Following is the example to explain the creation of List objects −

package com.tutorialspoint;

import java.util.*;
public class CollectionsDemo {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      System.out.println("List: " + list);
      List<String> list1 = Arrays.asList("Zara","Mahnaz","Ayan" );
      System.out.println("List1: " + list1);


This will produce the following result −

List: [Zara, Mahnaz, Ayan]
List1: [Zara, Mahnaz, Ayan]