Friday , April 28 2017
Home / java / ArrayList in Java

ArrayList in Java

The ArrayList in Java is a generic class that implements the List interface. This class provides a way to create dynamic arrays in Java. The ArrayList in Java implements all list operations, and permits all operations include duplicates.

Every ArrayList instance has a initial capacity, the capacity is the size of an array used to store the elements in the list. While adding the elements into ArrayList, it grows automatically.

Note: The ArrayList is not Thread safe i.e the operations that are implemented in the ArrayList are not synchronized. Where as Vector class in Thread safe.

If multiple Threads accessing ArrayList instance concurrently, and at least one of the thread changing the structure of the ArrayList, it must be synchronize. In such cases, we have 2 options in our hand, we may go with the Vector class or we can make the ArrayList as synchronize. We can make the ArrayList as synchronize by using Collections.synchronizedList.

List list = Collections.synchronizedList(new ArrayList(…));

Some of the key features of ArrayList include:

  • Ability to dynamically increase or decrease the size of an array as and when elements are added to it. Though ArrayList instances are created with an initial capacity, if the number of elements exceeds the capacity, the ArrayList is expanded automatically.

  • Ability to store all types of elements including null elements.

  • Support for all the operations provided by the List interface.

  • ArrayList maintains the insertion order.

The constructors for creating ArrayList instances are:

  • ArrayList():Creates an empty ArrayList instance with default initial capacity of 10.

  • ArrayList(Collection<? extends E>c):Creates an ArrayList instance and populates it with the contents of the collection c.

  • ArrayList(int capacity):Creates an ArrayList instance with the specified initial capacity.

Methods in ArrayList

The most commonly used methods in the ArrayList class are:

  • void add(int index, E obj):Inserts the element obj at the location specified by index. Replaces the existing element at that location.

  • E get(int index):Returns the element at the location specified by index.

  • E set(int index, E obj):Assigns the element obj to the specified location and returns the old element at that location.

  • boolean remove(int index):Removes the element at the specified location.

  • <T> T[] toArray(T[] a):Returns an array of type T that contains all the elements of the ArrayList instance a in the same sequence.

 Basic Operations in ArrayList in Java

package com.onlinetutorials;

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

public class ArrayListOperations {

    public static void main(String[] args) {
        List lst1 = new ArrayList();
        System.out.println("List1  : " + lst1);

        List lst2 = new ArrayList();
        System.out.println("List2  : " + lst2);

        // Adding an entire list to another list

        // Check whether a ArrayList contains an element or another List
        System.out.println("is contains : " + lst1.contains(10));
        System.out.println("is contains : " + lst2.containsAll(lst1));

        // Remove Elements from ArrayList

        System.out.println("List1 : " + lst1);
        System.out.println("List2 : " + lst2);

        // Updating ArrayList
        lst1.set(1, 0);

        System.out.println("List1 : " + lst1);

        System.out.println("0' th element : " + lst1.get(0));



List1  : [10, 9, 20]
List2  : [Sample, text, 9, 20]
is contains : true
is contains : true
List1 : [9, 20]
List2 : [Sample, text, 10]
List1 : [9, 0]
0' th element : 9

On the above example, we have taken two ArrayList objects and done basic operations on ArrayList. In the below example we can differentiate the difference between removeAll and retainAll operations on ArrayList.

removeAll() vs retainAll() :

package com.onlinetutorials;

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

public class RetainAllDemo {

    public static void main(String[] args) {
        List list = new ArrayList();
        List list2 = new ArrayList();
        System.out.println("List Before retainAll() list2 : " + list);
        System.out.println("List After retainAll() list2 : " + list);

        System.out.println("List After removeAll() : " + list);



List Before retainAll() list2 : [10, 20, 30, 40]
List After retainAll() list2 : [30, 40]
List After removeAll() : []

On the above example illustrates the difference between the removeAll() operation and retainAll() operation in ArrayList in java.  retainAll() operation removes from the list all of its elements that are not contained in the specified collection. Where as removeAll() operation removes from the list all of its elements that are contained in the specified collection.

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

Binary Literals Java 7 Example Tutorials

Binary literals is one of the new feature added in Java 7. We already know …

Leave a Reply

Your email address will not be published. Required fields are marked *