Java List – List in Java

As you know Arrays are inflexible and fixed-size data structure whereas List in Java is flexible, dynamic, and ordered collection.

Java List is a part of the collection framework where you can access List elements using some default methods. It gives you the functionality to access elements by their index, add, remove, and search elements.

Java List is an interface that extends the Collection interface and implemented by many classes such as AbstractList, ArrayList, LinkedList, etc.

List in Java are widely used during the development of any application and it can be found under java.util package.

Java List Overview:

  • It is a sequential collection.
  • The list is dynamic and growable in nature.
  • It is a part of Java Collection framework.
  • It allows duplicate elements.
  • It also allows NULL elements.
  • List elements can be accessed by their integer index (position).
  • ListIterator is used to iterate over the list and perform operation such as addition and removal.
  • It provides methods such as add(), addAll(), clear(), equal(), contains(), remove(), etc.
public interface List<E> extends Collection<E>

Java List Methods:

MethodDescription
1. boolean add(E e)It is used to add the element at the end of a list.
2. void add(int index, E element)It is used to insert the element at the specified position.
3. boolean equals(Object o)It is used to compare the specified object with the elements of a list.
4. boolean isEmpty()It returns true if the list is empty, otherwise false.
5. boolean contains(Object o)It returns true if the list contains the specified element
6. boolean remove(Object o)It is used to remove the first occurrence of the element.
7. int size()It is used to return the number of elements in the list.

Java List Examples:

Now, let’s see some example of above given methods:

1. How to create List in Java?

//Creating a List of type String. 
List<String> list=new ArrayList<String>();  
  
//Creating a List of type Integer.
List<Integer> list=new ArrayList<Integer>();

You can create list of any type. Whether String, Integer, or Custom Classes.

2. How to add elements in List?

public class JavaCollectionExample {

  public static void main(String[] args) {

    // Creating a List
    List<String> list = new ArrayList<String>();

    // Adding elements in the List
    list.add("John");
    list.add("Ronny");
    list.add("Peter");
    list.add("Olive ");

    // printing list object
    System.out.println(list);

  }
}

Above is the example of adding some names in the list using ArrayList class as an implmentation.

3. How to Convert List to Array in Java?

public class JavaCollectionExample {

  public static void main(String[] args) {

    // Creating a List
    List<String> list = new ArrayList<String>();

    // Adding elements in the List
    list.add("John");
    list.add("Ronny");
    list.add("Peter");
    list.add("Olive ");

    // converting list to array
    String[] array = list.toArray(new String[list.size()]);

    // printing list object
    System.out.println(list);

    // printing array object
    System.out.println(Arrays.toString(array));

  }
}

In this example list is converted to array using toArray() method of list and size of array is defined using .size() method which defines length(size) of list.

4. How to convert String Array to List?

Method 1: Using .asList() method

    // Creating an array
    String[] array = { "John", "Ronny", "Peter", "Olive" };

    // converting array to list
    List<String> list = Arrays.asList(array);

    // printing list
    System.out.println(list);

Method 2: Using forloop

public class JavaCollectionExample {

  public static void main(String[] args) {
    
    // Creating an array
    String[] array = { "John", "Ronny", "Peter", "Olive" };

    // Creating a List
    List<String> list = new ArrayList<String>();

    // converting array to list
    for (String obj : array) {
      list.add(obj);
    }

    // printing list
    System.out.println(list);
  }
}

In method 1, an array is converted to a list using the asList() method.

In method 2, each element of the array is iterated and then copied to list which is safe when doing modification at the same time.

Happy Learning!

JSP Syntax

ArrayList in Java