HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

A Guide to ArrayList in Java

An ArrayList in Java represent a resizable list of objects. We can add, remove, find, sort and replace elements in this list. ArrayList is part of Java’s collection framework and implements Java’s List interface.

Hierarchy of ArrayList class

Java ArrayList class extends AbstractList class which implements List interface. The List interface extends Collection and Iterable interfaces in hierarchical order.

ArrayList Hierarchy
ArrayList Hierarchy

1. ArrayList Features

ArrayList has following features –

  1. Ordered – Elements in arraylist preserve their ordering which is by default the order in which they were added to the list.
  2. Index based – Elements can be randomly accessed using index positions. Index start with '0'.
  3. Dynamic resizing – ArrayList grows dynamically when more elements needs to be added than it’s current size.
  4. Non synchronized – ArrayList is not synchronized, by default. Programmer needs to use synchronized keyword appropiately or simply use Vector class.
  5. Duplicates allowed – We can add duplicate elements in arraylist. It is not possible in sets.

2. Internal Working of ArrayList

ArrayList class is implemented arounda backing array. The elements added or removed from arraylist are actually modified in this backing array. All arraylist methods access this array and get/set elements in the array.

ArrayList basically cab be seen as resizable-array implementation in Java.

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, 
        		   Cloneable, java.io.Serializable
{
	transient Object[] elementData;		//backing array
	private int size;					//array or list size

	//more code
}

3. ArrayList Examples

3.1. Create ArrayList

Usually we will create either an empty list and add elements to it. Or we will create an arraylist with another exising collection.

//Empty arraylist
List<String> names = new ArrayList<>(); 

//Arraylist initialized with another collection
List<Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5)); 

3.2. Add and remove elements

Use add(), set() and remove() methods to add or update the elements in the list.

//Create arraylist
List<String> names = new ArrayList<>(); 

names.add("lokesh");    //[lokesh]
names.add("alex");      //[lokesh, alex]

names.set(1, "brian");  //[lokesh, brian]

names.remove(1);        //[lokesh]

3.2. Iterate

Use iterator() or listIterator() to get the reference of iterator instance. This iterator can be used to iterate the elements in the arraylist.

ArrayList<Integer> digits = new ArrayList<>(Arrays.asList(1,2,3,4,5,6));

Iterator<Integer> iterator = digits.iterator();

while(iterator.hasNext()) {
	System.out.println(iterator.next());
}

Program output.

1
2
3
4
5
6

4. ArrayList Methods

ArrayList add() method example
ArrayList addAll() method example
ArrayList clear() method example
ArrayList clone() – How to clone an ArrayList
ArrayList contains() method example
ArrayList ensureCapacity() method example
ArrayList forEach() method example
ArrayList get() method example
ArrayList indexOf() method example
ArrayList lastIndexOf() method example
ArrayList listIterator() method example
ArrayList remove() method example
ArrayList removeAll() method example
ArrayList retainAll() method example
ArrayList replaceAll() method example
ArrayList removeIf() method example
ArrayList sort() method example
ArrayList spliterator() method example
ArrayList subList() method example
ArrayList toArray() method example

5. Java ArrayList Examples

5.1. Create arraylist

Initialize ArrayList
Iterate through ArrayList

5.2. Add elements and remove elements

Add element at particular index of ArrayList
Remove element from ArrayList
Add multiple items to ArrayList

5.3. Sort arraylist

Sort ArrayList
Sort ArrayList of Objects using Comparable and Comparator
Sort ArrayList of objects by multiple fields
Sort ArrayList of objects using Collections.sort() method

5.4. Get/Search

Get Sub List of ArrayList
Find the index of last index of the element in the ArrayList
Get the index of the element in the ArrayList
Get element from ArrayList
Check if element exists in ArrayList

6. Other Tutorials on Java ArrayList

Compare two ArrayLists
Synchronize ArrayList
Swap two elements in ArrayList
Serialize ArrayList
Join two ArrayList
Make ArrayList Empty
Check whether ArrayList is empty or not
Replace the value of existing element in ArrayList
Remove duplicate elements in ArrayList

7. Conversions

Convert LinkedList to ArrayList
Convert Vector to ArrayList
Convert ArrayList to String Array
Convert Array to ArrayList
Convert HashSet to ArrayList

8. Differences

ArrayList vs Vector
ArrayList vs LinkedList

References:

ArrayList Java Docs

TwitterFacebookLinkedinRedditPocket

Search Tutorials

ArrayList Methods

  • ArrayList – Introduction
  • ArrayList – add()
  • ArrayList – addAll()
  • ArrayList – clear()
  • ArrayList – clone()
  • ArrayList – contains()
  • ArrayList – ensureCapacity()
  • ArrayList – forEach()
  • ArrayList – get()
  • Arraylist – indexOf()
  • Arraylist – lastIndexOf()
  • ArrayList – listIterator()
  • ArrayList – remove()
  • ArrayList – removeAll()
  • ArrayList – removeIf()
  • ArrayList – retainAll()
  • ArrayList – sort()
  • ArrayList – spliterator()
  • ArrayList – subList()
  • ArrayList – toArray()

ArrayList Examples

  • ArrayList – Initialize arraylist
  • ArrayList – Iteration
  • ArrayList – Add/replace element
  • ArrayList – Add multiple elements
  • ArrayList – Check empty list
  • ArrayList – Remove element
  • ArrayList – Replace element
  • ArrayList – Empty arraylist
  • ArrayList – Synchronized arraylist
  • ArrayList – Compare two lists
  • ArrayList – Remove duplicates
  • ArrayList – Merge two lists
  • ArrayList – Serialization
  • ArrayList – Swap two elements
  • Convert ArrayList to Array
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • Convert Vector to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Java Collections

  • Collections Framework
  • Array
  • ArrayList
  • LinkedList
  • HashMap
  • Hashtable
  • LinkedHashMap
  • TreeMap
  • HashSet
  • LinkedHashSet
  • TreeSet
  • Comparable
  • Comparator
  • Iterator
  • ListIterator
  • Spliterator
  • PriorityQueue
  • PriorityBlockingQueue
  • ArrayBlockingQueue
  • LinkedTransferQueue
  • CopyOnWriteArrayList
  • CopyOnWriteArraySet
  • Collection Sorting
  • Interview Questions

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap