Wednesday , June 28 2017
Home / java / ArrayList in Java

ArrayList in Java

In this tutorials, we are going to discuss what is ArrayList in Java and how to work with ArrayList.

Java ArrayList :

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

Every ArrayList instance has an initial capacity of 10, 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 if one of the thread changing the structure of the ArrayList it will be effected by all other threads. In such cases, we have 2 options in our hand, we may go with the Vector class (since it is synchronized) 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 to create ArrayList :

  • 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

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

}

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

removeAll() vs retainAll() :

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.


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() : []

Happy Learning 🙂

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 *