What is a Java list?

Java lists are particularly suitable for scenarios where the size of a data set is not clear from the outset or if the size is expected to change over time. We’ll show you specific examples of how to use Java lists as well as the operations that you can perform with them.

What can Java lists be used for?

Lists are one of the fundamental data structures in Java programming and can be used in a variety of ways. They contain elements, which are arranged in an ordered sequence. You can add, modify, delete or query elements in a list. Java lists can contain objects that belong to different classes. It’s also possible to store duplicate elements or null elements. Additionally, Java lists support generic classes and methods, ensuring type safety.

Lists can be used for database applications. Here they are used to store and access large data sets from database queries. In graphical user interfaces, Java lists are often used to display a list of elements, such as options in a drop-down menu or various items in an online shop.

Java lists are also indispensable in algorithms and data structures. They are used in sorting algorithms, search algorithms and stack and queue structures. In network applications, lists can help facilitate the management of connections and sockets.

Which list methods does Java have?

Java lists belong to the Collection interface and must be imported from the package java.util. With Java ArrayList, LinkedList, Vector, and Stack, there are various implementation classes that you can choose from. You can declare the different instances of the lists with the following code:

List linkedList = new LinkedList(); // LinkedList
List arrayList = new ArrayList(); // ArrayList
List vecList = new Vector(); // Vector
List stackList = new Stack(); //Stack
Java

Here are some of the most important methods you can use for Java lists:

  1. int size(): Determines the number of elements in a list
  2. void add(int index, E element): Adds an element at a specific position
  3. boolean isEmpty(): Checks if a list is empty
  4. void clear(): Removes all elements from a list
  5. boolean contains(Object o): Returns the value true, if the object o is in the list
  6. boolean add(E e): Adds an element to the end of a list
  7. boolean remove(Object o): Removes the first occurrence of an element
  8. E get(int index): Replaces or inserts an element at a specified index
  9. E set(int index, E element): Replaces or inserts an element at a specified index
  10. Object[] toArray(): Returns an array containing the elements from a list
  11. List<E> subList(int fromIndex, int toIndex): Captures all elements within the specified interval
  12. default void replaceAll(UnaryOperator<E> operator): Standard method in Java 8 that applies unary Java operators to each element and then replaces each element with the result from the operation.

How to use Java lists

Below, we’ll demonstrate common methods that are used when working with Java lists. These include converting arrays into lists and vice versa, as well as sorting, retrieving and modifying elements.

Convert an array into a list

To convert an array into a list, you can iterate through the array using loops and add the elements to the list one by one using the add() method.

import java.util.*;
    public class ArrayToList{
      public static void main(String args[]){
      // Creating Array
      String[] colors={"blue","green","red","yellow"};
      System.out.println("Array: "+Arrays.toString(colors));
      //Converting Array to List
      List<String> list=new ArrayList<String>();
      for(String color: colors){
        list.add(color);
      }
      System.out.println("List: "+list);
      }
    }
Java

This results in the output:

Array: [blue, green, red, yellow]
List: [blue, green, red, yellow]
Java

Convert a list into an array

With the method toArray() , you can convert a list into an array:

import java.util.*;
    public class ListToArray{
      public static void main(String args[]){
       List<String> days = new ArrayList<String>();
       days.add("Monday");
       days.add("Tuesday");
       days.add("Wednesday");
       days.add("Thursday");
       days.add("Friday");
       days.add("Saturday");
       days.add("Sunday");
       // Converting ArrayList to Array
       String[] array = days.toArray(new String[days.size()]);
       System.out.println("Array: "+Arrays.toString(array));
       System.out.println("List: "+days);
      }
    }
Java

In the output, you can see that the elements in the array and the list are identical:

Array: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
List: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
Java

Call and change elements in a Java list

With the get() method, you can access an element at a specific index. The set() methods lets you replace an element with a different element at a specified position in the list.

import java.util.*;
    public class ListExample{
      public static void main(String args[]){
      // Creating a List
      List<String> letters=new ArrayList<String>();
      // Adding elements
      letters.add("a");
      letters.add("b");
      letters.add("c");
      // get()
      System.out.println("Element at index 1: "+letters.get(1));
      // set()
      letters.set(2, "d");
      for(String letter: letters)
      System.out.println(letter);
      }
    }
Java

Since counting in Java starts with the index 0, we get the following result:

Element at index 1: b
a
b
d
Java

Sort a list

You can sort a Java list using the .sort() method from the Collections class. You can iterate through the list using a loop and print the elements to the console one by one:

import java.util.*;
    class SortArrayList{
      public static void main(String args[]){
      // Creating a list of numbers
      List<Integer> numbers=new ArrayList<Integer>();
      numbers.add(4);
      numbers.add(57);
      numbers.add(92);
      numbers.add(26);
      // Sorting
      Collections.sort(numbers);
      for(Integer number: numbers)
        System.out.println(number);
      }
    }
Java

The numbers in the Java List are displayed on the screen from smallest to largest:

4
26
57
92
Java
Was this article helpful?
Page top