Wednesday , January 18 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();
        lst1.add(10);
        lst1.add(9);
        lst1.add(20);
        System.out.println("List1  : " + lst1);

        List lst2 = new ArrayList();
        lst2.add("Sample");
        lst2.add("text");
        lst2.add(9);
        lst2.add(20);
        System.out.println("List2  : " + lst2);

        // Adding an entire list to another list
         
        lst2.addAll(lst1);

        // 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
        lst1.remove(0);
        lst2.removeAll(lst1);

        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));
        
    }

}

Output:

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.add(10);
        list.add(20);
        list.add(30);
        list.add(40);
        List list2 = new ArrayList();
        list2.add(30);
        list2.add(40);
        System.out.println("List Before retainAll() list2 : " + list);
        list.retainAll(list2);
        System.out.println("List After retainAll() list2 : " + list);

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

    }
}

Output:

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

Type Inference in Java 7 Example

Type Inference in Java 7 is another great addition introduced to ease the developer to type …

Leave a Reply

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