Tutorials Hut

Java ArrayList 

Java ArrayList is a class which extends AbstractList which implements List interface. ArrayList can store non-primitive data types as elements and form a list. ArrayList is an ordered list that means elements are arranged in the same order they are added.

  • ArrayList may contain duplicate elements.
  • ArrayList is not synchronized.
  • ArrayList manipulation is slower than LinkedList because we have to do a lot of shifting once an element is removed.
  • ArrayList dynamically increases its capacity based on requirement.

ArrayList Declaration

Non Generic Style

ArrayList myList = new ArrayList();

Generic Styles

ArrayList myList = new ArrayList(); //other way

ArrayList Constructors

Constructor

Description

ArrayList()

Creates empty list

ArrayList(Collection<? extends E> c)

ArrayList initialized with some collection.

ArrayList(int capacity)

ArrayList with predefined capacity.

ArrayList Methods

Methods

Detail

void add(int index, E element)

Adds an element at specific index in list.

boolean add(E e)

Adds an element in list.

boolean addAll(Collection<? extends E> c)

Adds a collection in list.

boolean addAll(int index, Collection<? extends E> c)

Ads a collection at a specifc index.

void clear()

Clears list.

void ensureCapacity(int requiredCapacity)

It is used to set or ensure list capacity.

E get(int index)

Returns element of given index.

boolean isEmpty()

Checks if list is empty

Iterator()

Iterator

listIterator()

listIterator

int lastIndexOf(Object o)

Finds last index of object.

Object[] toArray()

Converts to Array.

T[] toArray(T[] a)

Returns Array.

Object clone()

Creates shallow clone of list.

boolean contains(Object o)

Checks if contains element.

int indexOf(Object o)

Returns index of element.

E remove(int index)

Removes the element at given index.

boolean remove(Object o)

Removed object.

boolean removeAll(Collection<?> c)

Removes all collection.

boolean removeIf(Predicate<? super E> filter)

Removed based on condition.

protected void removeRange(int fromIndex, int toIndex)

Removes range of elements.

void replaceAll(UnaryOperator operator)

Replaces.

void retainAll(Collection<?> c)

Retains all elements.

E set(int index, E element)

Sets element at index.

void sort(Comparator<? super E> c)

It sorts based on passed comparator.

Spliterator spliterator()

It is used to create spliterator.

List subList(int fromIndex, int toIndex)

It returns sub list based on index.

int size()

Returns size.

void trimToSize()

Trims to size.

ArrayList Example With Iterating Using Iterator()

import java.util.*;

class ListExamples{
public static void main(String args[]){
//create list
ArrayList list=new ArrayList();

//add elemennts in list
list.add("Tom");
list.add("Bob");
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}

Output

Tom
Bob

Iterating  ArrayList or collection with forEach, for, ListIterator
 and forEachRemaining

Example:

import java.util.ArrayList;
import java.util.ListIterator;

public class ArrayListExamples {
public static void main(String[] args) {
    ArrayList myList=new ArrayList<>();
    myList.add("John");
     myList.add("Ron");

    //foreach
     myList.forEach(data->System.out.println("forEach: "+data));

    //for
    for (String data : myList) {
        System.out.println("for: "+data);
     }

    //list itr
    ListIterator ltr = myList.listIterator();
    while(ltr.hasNext()) {
        System.out.println("listIterator: "+ltr.next());
    }

    //forEachRemaining of iterator
    ltr.forEachRemaining(data->{
      System.out.println("forEachRemaining: "+data);
});
}
}

 

Output:

forEach: John
forEach: Ron
for: John
for: Ron
listIterator: John
listIterator: Ron

List/Collection examples of add, addAll, remove, removeAll and retainAll()

import java.util.ArrayList;
public class ArrayListExample2 {
public static void main(String[] args) {
    ArrayList myList=new ArrayList<>();
    ArrayList myList2=new ArrayList<>();

    //add
    myList.add("John");
    myList.add("Ron");
    myList.add("Jone");
    myList.add("Tony");
    myList2.add("Tony");
  System.out.println(myList);

    //addAll
   myList.addAll(myList2);

   //remove
     myList.remove("John");

    //removeAll
    myList.removeAll(myList2);
    System.out.println(myList);
    //retainAll
    myList.add("Tony");
    myList.retainAll(myList2);
    System.out.println(myList);
}
}

 

Output

[John, Ron, Jone, Tony]
[Ron, Jone]
[Tony]

Sorting the ArrayList

We can use collections class’ sorting methods to sort it.

Syntax:

 Collections.sort();  

Next Article

  •  LinkedList