How do you create an empty 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<>();

Example

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<>();
      list.add("Zara");
      list.add("Mahnaz");
      list.add("Ayan");
      System.out.println("List: " + list);
   }
}

Output

This will produce the following result −

List: [Zara, Mahnaz, Ayan]

Updated on: 13-Sep-2023

33K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements