HOWTO · Java

How to Return an ArrayList in Java

In this tutorial, we will return an ArrayList from a function in Java.

On this page

An ArrayList is a resizable collection in Java’s java.util package. To return one, declare an ArrayList return type, create and populate the collection, then return it with return. In Java, functions declared in a class are called methods. Choose an instance method when the operation uses an object’s state, or a static method when the caller should use the class directly. If callers need only general list operations, a method can declare the List<T> interface as its return type while returning an ArrayList<T> object.

Return an ArrayList From a Non-Static Function in Java

A non-static method belongs to an instance of its class. The caller first constructs that class, then invokes the method on the resulting object. In this example, ClassB creates a list of integers and Main receives the returned collection in a variable declared as List<Integer>. The List interface is a useful variable type when the caller only needs list operations, while the method can declare the concrete ArrayList<Integer> type.

The complete program uses one public class, with ClassB nested inside it so the example can be saved and compiled as a single Java source file. The main method is the caller: it creates ClassB, invokes myNumbers() through m1, and prints the returned values.

import java.util.ArrayList;
import java.util.List;

public class Main {
  static class ClassB {
    public ArrayList<Integer> myNumbers() {
      ArrayList<Integer> numbers = new ArrayList<>();
      numbers.add(10);
      numbers.add(15);
      numbers.add(30);
      return numbers;
    }
  }

  public static void main(String[] args) {
    ClassB m1 = new ClassB();
    List<Integer> listInMain = m1.myNumbers();
    System.out.println("The List is " + listInMain);
  }
}
The List is [10, 15, 30]

The generic type Integer describes the elements held by the collection. The method creates a fresh list on each call, adds three numbers, and returns that list. The caller can read, iterate over, or add values to the returned collection. The method returns a reference to the list; it does not convert the collection into an array. Because the caller receives that same object, changes made through the returned reference affect the list created by the method.

Return an ArrayList From a Static Function in Java

A static method belongs to the class rather than to a particular object. Call it with the class name and method name, so this version does not construct ClassB. This is useful when the method’s work does not depend on fields stored on a specific instance. Its body still creates and returns an ArrayList in the same way as the instance method.

This standalone program uses the same data so you can focus on the call form. Since the method is declared static, Main invokes it as ClassB.myNumbers(). The returned value is assigned to a List<Integer> variable and printed by the caller.

import java.util.ArrayList;
import java.util.List;

public class Main {
  static class ClassB {
    public static ArrayList<Integer> myNumbers() {
      ArrayList<Integer> numbers = new ArrayList<>();
      numbers.add(10);
      numbers.add(15);
      numbers.add(30);
      return numbers;
    }
  }

  public static void main(String[] args) {
    List<Integer> listInMain = ClassB.myNumbers();
    System.out.println("The List is " + listInMain);
  }
}
The List is [10, 15, 30]

Use an instance method when the operation needs an object’s state, and a static method when it is class-level and needs no instance. In either case, make the declared return type compatible with the value and assign the result to a compatible type at the call site. The return statement ends the method and gives the populated list to the caller, which can use normal List operations.