Introduction to the Java ArrayList | Java Development Journal

Introduction to the Java ArrayList

In this post, we will cover some of the features of Java ArrayList class. We will discuss its features and well-known use cases.

 

Java ArrayList

The ArrayList class is the part of Java Collection Framework. This class implements the List interface provided by the Collection framework.ArrayList grow and sink dynamically when we add/remove elements from it. This provides flexibility to use elements using the indexes. Here are some of the known parameters for the Java ArrayList complexity.

  • Add operation has O(1) complexity.
  • Remove has O(1) complexity.
  • Contains operation have O(n) complexity.

Internally ArrayList is a resizable-array implementation of the List interface. Here is a sample code with ArrayList

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

public class ArrayListExample {

 public static void main(String[] args) {

  List < String > list = new ArrayList < String > ();

  //Add operation
  list.add("California");
  list.add("Alabama");
  list.add("Colorado");
  list.add("Delaware");
  list.add(4, "Texas"); //add based on index

  list.contains("Virginia"); //false

  list.remove("Colorado");
  list.remove(1); //remove by index
 }
}

Let’s take a closer look at the ArrayList

 

1. ArrayList Creation

On a very high level, we have the following way to create a new ArrayList in Java.

  • Use default constructor.
  • Pass initial capacity in the constructor.
  • Pass Java Collection as constructor parameter.

 

1.1 Default Constructor

This is one of the most common ways to create ArrayList in Java. We are creating an empty ArrayList.

List<String> list= new ArrayList<String>();

 

1.2  Constructor with Initial Capacity

This option allows us to constructs an empty list with the specified initial capacity. This is helpful to avoid frequent resizing during the add operation

List<String> list= new ArrayList<String>(10);

 

1.3  Constructor with Collection

This method helps to Constructs a list containing the elements of the specified collection.

List<String> intialList = Arrays.asList("California","Alabama");
List<String> list= new ArrayList<String>(intialList)

While creating ArrayList, we should keep in mind following extra points.

  • ArrayList is generic and it’s always recommended to pass element type to let compiler make sure data integrity.
  • ArrayList implements List interface and it’s a good practice to use generic interface for better decoupling.

 

2. Add Elements

We can add elements by calling add() method or if required we can add an element at the specified position using index property. 

List<String> list= new ArrayList<String>();
list.add("California");
list.add(4,"Texas");

We can also add multiple elements at once.

List<String> intialList = Arrays.asList("California1","Alabama1");
List<String> list= new ArrayList<String>();
list.addAll(intialList);

 

3. Remove Elements

There are two variations of remove operation in ArrayList.

  • Use remove() method to remove an element by value or using an index.remove() by Object will remove the first occurrence of the specified element from this list.
  • Use removeAll() to removes from this list all of its elements that are contained in the specified collection.
List < String > list = new ArrayList < String > ();
list.addAll(intialList);
//Add operation
list.add("California");
list.add("Alabama");
list.add("Colorado");
list.add("Delaware");

list.remove("Colorado"); //By value
list.remove(1); //remove by index

List < String > removeCollection = Arrays.asList("California", "Alabama");
list.removeAll(removeCollection);

 

4. Iterate ArrayList Elements

We have two different ways to iterate elements of the Java ArrayList.

  • Iterator – Provide option to iterate a list in one direction.
  • Listiterator – An iterator for lists that allows the programmer to traverse the list in either direction
List < Integer > integerList = Arrays.asList(1, 2, 3, 6, 7, 4, 9, 11, 34, 12, 22);
 ListIterator < Integer > listIterator = integerList.listIterator();
 while (listIterator.hasNext()) {
  System.out.println("Next element is " + listIterator.next());
 }
 while (listIterator.hasPrevious()) {
  System.out.println("Previous element is " + listIterator.previous());
 }

Java 8 brought a number of fundamental changes to the API.Use forEach() method directly in the list to performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

 

5. Search

ArrayList provides a number of options to search elements through the list.

  • Use indexOf() and lastIndexOf() methods to get index element for the given method.
  • contains(Object) – Returns true if this list contains the specified element.
List < String > stringList = Arrays.asList("California", "Alabama", "Colorado", "Delaware", "Virginia");
assertEquals(1, stringList.indexOf("Alabama"));

If your code requires the frequent check for specific elements, better to use a HashSet than an ArrayList.Java doc for HashSet says: “This class offers constant time performance for the basic operations (add, remove, contains and size)”

 

Summary

In this post, we discussed the ArrayList in Java. We learned how to create a new ArrayList in Java and how to do different operations on the ArrayList.

Umesh

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of