ArrayList in Java

ArrayList in Java

In this tutorial, we are going to discuss the ArrayList in java. The ArrayList class is a resizable array, which can be found in java.util package.

ArrayList in Java
  • It uses a dynamic array for storing the elements. It extends AbstractList class and implements the List interface.
  • ArrayLists are created with initial size, and when this size is exceeded, it gets enlarged automatically.
  • Can contain duplicate elements.
  • Maintains insertion order.
  • Not synchronized.
  • Random access because the array works at the index basis.
  • Manipulation slows because a lot of shifting needs to be occurred if any element is removed from the array list.
  • null insertion is possible.
  • Heterogeneous objects are allowed.
Constructors
1. ArrayList a=new ArrayList();

Creates an empty ArrayList object with default initial capacity “10” if ArrayList reaches its max capacity, then a new ArrayList object will be created with

New capacity=(current capacity*3/2)+1
2. ArrayList a=new ArrayList(int initialcapacity);

Creates an empty ArrayList object with the specified initial capacity.

3. ArrayList a=new ArrayList(collection c);

Creates an equivalent ArrayList object for the given Collection that is this constructor meant for inter conversation between collection objects. That is to dance between collection objects.

package com.ashok.collections;

import java.util.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyArrayList {
   public static void main(String[] args) {
      ArrayList a=new ArrayList();
      a.add("Ashok");
      a.add(10);
      a.add("Ashok");
      a.add(null);
      System.out.println(a);//[Ashok, 10, Ashok, null]
      a.remove(2);
      System.out.println(a);//[Ashok, 10, null]
      a.add(2,"Vinod");
      a.add("Kimu");
      System.out.println(a);//[Ashok, 10, Vinod, null, Kimu]
   }
}

Note

In every collection class toString() is overridden to return it’s content directly in the following format.

[object1, object2, object3, ......]
  • Usually, we can use the collection to hold and transfer objects from one tier to another tier. To provide support for this requirement, every Collection class already implements Serializable and Cloneable interfaces.
  • ArrayList and Vector classes implement the RandomAccess interface to access any random element with the same speed. Hence ArrayList is the best choice of “retrival operation.”
  • RandomAccess interface present in util package and doesn’t contain any methods. It is a marker interface.
package com.ashok.collections;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.RandomAccess;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      ArrayList arrayList = new ArrayList();
      LinkedList linkedList = new LinkedList();

      System.out.println(arrayList instanceof Serializable); // true
      System.out.println(linkedList instanceof Cloneable); // true
   
      System.out.println(arrayList instanceof RandomAccess); // true
      System.out.println(linkedList instanceof RandomAccess); // false
   }
}
ArrayList Vs Vector
1. Synchronization

ArrayList is non-synchronized, which means multiple threads can work on ArrayList at the same time. For example, if one thread performs an add operation on ArrayList, another thread can perform a remove operation on ArrayList simultaneously in a multithreaded environment.

While Vector is synchronized, if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on Vector at a time.

2. Resize

Both ArrayList and Vector can grow and shrink dynamically to maintain the optimal use of storage. However, the way they resized is different. ArrayList grows by half of its size when resized, while Vector doubles its size by default when it grows.

3. Performance

ArrayList gives better performance as it is non-synchronized. Vector operations give poor performance as they are thread-safe. The thread that works on Vector gets a lock on it, making other threads wait until the lock is released.

How to get synchronized version of ArrayList object

Collections class defines the following method to return the synchronized version of List.

public static List synchronizedList(list l);
Capture 15

Similarly, we can get a synchronized version of Set and Map objects by using the following methods.

1. public static Set synchronizedSet(Set s);
2. public static Map synchronizedMap(Map m); 
  • ArrayList is the best choice if our frequent operation is retrieval.
  • ArrayList is the worst choice if our frequent operation is an insertion (or) deletion in the middle because it requires several internal shift operations.
Capture2
ArrayList in Java
Scroll to top