How to use Java ArrayList with Examples

Tags: Java ArrayList

In this tutorial we will go through a list of examples to show you how use java.util.ArrayList in Java program. You will learn how to adding, updating, getting, sorting and other operations on elements of an ArrayList. ArrayList is an implementation of List interface with class diagram as below:

Java ArrayList class diagram

How to add one element to an ArrayList

There are two approaches to add an element to ArrayList which is append it to the end of the list or you can insert it to specified index position

List<String> list = new ArrayList<>();
list.add("Java");
list.add("C");
list.add("C++");
System.out.println(list);

list.add(1, "JavaScript");
System.out.println(list);

How to add a list of elements to an ArrayList

There are also two approaches to add list of elements to ArrayList

List<String> list1 = new ArrayList<>();
list1.add("Java");
list1.add("C");
list1.add("C++");

List<String> list2 = new ArrayList<>();
list2.add("Python");
list2.add("Go");

// Add all elements of list2 to list1
list1.addAll(list2);
System.out.println(list1);

List<String> list3 = new ArrayList<>();
list3.add("C#");
list3.add("JavaScript");

// Add all elements of list3 to list1 at a specified index
list1.addAll(2, list3);
System.out.println(list1);

Different methods to access element of an ArrayList

List<String> list = new ArrayList<>();
list.add("Java");
list.add("C");
list.add("C++");
list.add("Python");
list.add("Go");
list.add("C#");
list.add("JavaScript");
list.add("Python");

// get element by its index
System.out.println(list.get(0));

// get index of specified element
System.out.println(list.indexOf("C++"));

// get last index in list of an specified element
System.out.println(list.lastIndexOf("Python"));

// get a sub list of elements by start and end index
System.out.println(list.subList(2, 4));

Checking element is contains in an ArrayList

List<String> list = new ArrayList<>();
list.add("Java");
list.add("C");
list.add("C++");

System.out.println(list.contains("Java"));
System.out.println(list.contains("JavaScript"));

Checking an ArrayList if empty or not

List<String> list = new ArrayList<>();
// check the list is empty or not before adding element 
System.out.println(list.isEmpty());

list.add("Java");
// check the list is empty or not after adding element
System.out.println(list.isEmpty());

Updating elements of an ArrayList

There are different methods to update elements of ArrayList:

  • retainAll to update ArrayList that retain only elements that include in a collection in argument
  • set to replace the element at specified index
  • replaceAll to replace all element of a list with the result of lambda expression
  • size to get the size of an ArrayList
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("C");
list.add("C++");
list.add("Python");
list.add("Go");
list.add("C#");
list.add("JS");

System.out.println(list);

List<String> list2 = new ArrayList<>();
list2.add("Java");
list2.add("JS");
// Retain in list which element exist in list2
list.retainAll(list2);
System.out.println(list);

// Update element from JS to JavaScript
list.set(1, "JavaScript");

System.out.println(list);
// Replace all element which result return from lambda expression
list.replaceAll(element -> element + " Programming Language");
System.out.println(list);

// get the size of ArrayList
System.out.println(list.size());

Removing elements from an ArrayList

There are 5 different methods to remove elements from an ArrayList

  • remove(int index) to remove an element at a specified index
  • remove(Object element) to remove the first occurrence element
  • removeIf to remove all element that satisfy the lambda expression
  • removeAll to remove all element existing in argument collection
  • clear to remove all element of ArrayList
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("C");
list.add("C++");
list.add("Python");
list.add("Go");
list.add("C#");
list.add("JavaScript");
list.add("Kotlin");

System.out.println(list);

// Remove by index
list.remove(3);
System.out.println(list);

// Remove by element value
list.remove("JavaScript");
System.out.println(list);

// Remove if the lambda expression return true
list.removeIf(element -> element.startsWith("C"));
System.out.println(list);

List<String> list2 = new ArrayList<>();
list2.add("Go");
list2.add("Kotlin");
// Remove all element in list that exist in list2
list.removeAll(list2);
System.out.println(list);

// Remove all elements
list.clear();
System.out.println(list);

Different approaches to loop through elements of an ArrayList

In below example we are showing you 4 different solutions to loop elements of an ArrayList in Java.

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);

System.out.println("Solution 1: Loop Element using for loop");
for(int i = 0 ; i < list.size(); i++) {
    System.out.println(list.get(i));
}

list.forEach(element -> System.out.println(element) );

System.out.println("Solution 2: Loop Element using listIterator");
ListIterator<Integer> listIterator = list.listIterator();
while (listIterator.hasNext()) {
    System.out.println(listIterator.next());
}

System.out.println("Solution 3: Loop Element using iterator");
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

System.out.println("Solution 4: Loop Element using spliterator");
Spliterator<Integer> spliterator = list.spliterator();
spliterator.forEachRemaining(element -> System.out.println(element));

How to sort an ArrayList

In below example we are showing you 2 solutions to sort elements in an ArrayList.

ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("C");
list.add("C++");
list.add("Python");
list.add("Go");
list.add("C#");
list.add("JavaScript");

System.out.println(list);

Collections.sort(list, Collections.reverseOrder());

System.out.println(list);

list.sort(new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return o1.compareTo(o2);
    }
});

System.out.println(list);

Convert ArrayList to Java array

ArrayList class provides 2 methods to allow you convert an ArrayList object into an array.

ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("C");
list.add("C++");
list.add("Python");
list.add("Go");
list.add("C#");
list.add("JavaScript");

Object[] objectArray = list.toArray();
System.out.println(objectArray.length);

String[] stringArray = list.toArray(new String[0]);
System.out.println(stringArray.length);

That’s all for a simple solution examples how to use java.util.ArrayList class in Java.

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/ArrayListExamples

Happy Coding 😊

Java Create ArrayList using List Interface

Java Convert ArrayList to Array

Java Convert ArrayList to String

Java Convert ArrayList to Comma Separated String

Java Convert ArrayList to HashSet

Java Convert ArrayList to LinkedList

Convert Array to List in Java

Java Get Index of Minimum Value in ArrayList

Java Create New Array with Class Type and Length