Lists – Array List and Linked List

In this section we will be discussing about the two kinds of lists that would be helpful in storing and manipulating the elements.

This list contains various methods that perform many actions such as, insert, update, delete, and search upon the elements.

Array List –

Arrays list refers to an array kind that helps in working with a bunch of elements. When compared to general arrays that have a fixed size, it offers a huge advantage in terms of its dynamic.

It is provided under the java.util package. It is easier to handle and work with as compared to an array.

Some points about the Array List that we must take note of:

  • It can store duplicate elements.
  • The insertion order is maintained internally.
  • It can access random elements on the basis of the index values.
  • It is non- synchronized.
  • Implements the List interface.
  • It inherits the Abstract List.

Syntax to Declare the ArrayList

ArrayList<String> arayElements = new ArrayList<>();

Manipulations that can be performed using ArrayList

As we said that these lists could help us in performing various kinds of operations on elements. Let’s have a look at some of these operations that could be performed on the Array List.

  1. Adding Elements

With the Array List we can add elements to a new or an existing Array List. We is used to add() method to perform the addition operation. It provides the following methods:

  • add(Object) – It adds the element at the end of the List
  • add(int index, Object) – It adds the element at the particular index in the List.

Example-

import java.util.ArrayList;

public class Test {

	public static void main(String args[]) {
		ArrayList<String> arayElements = new ArrayList<>();

		arayElements.add("QA");
		arayElements.add("TechHub");

		System.out.println("Before adding new element" + arayElements);

		arayElements.add(0, "Welcome");

		System.out.println("After adding new element" + arayElements);
	}
}

Output-

Before adding new element[QA, TechHub]
After adding new element[Welcome, QA, TechHub]
  1. Removing Elements

We can remove the elements from the Array List, by using remove() method. It provides the following methods :

  • remove(Object) – It removes the element from the ArrayList. If there are similar kinds of elements, then the first occurrence of that object is deleted.
  • remove(int index) – It takes the index value as the input, and whichever element is placed at that location is removed from the ArrayList.

Example-


import java.util.ArrayList;

public class Test {

	public static void main(String args[]) {
		ArrayList<String> arayElements = new ArrayList<>();

		arayElements.add("QA");
		arayElements.add("TechHub");
		arayElements.add(0, "Welcome");
		System.out.println("Before removing any element" + arayElements);

		arayElements.remove("Welcome");

		System.out.println("After removing element" + arayElements);
	}
}

Output-

Before removing any element[Welcome, QA, TechHub]
After removing element[QA, TechHub]
  1. Update Elements

Once we have added the elements to the Array List, we may want to update the elements. We can do the same by using the set () method.

 Following is the method provided for it:

  • set(int index, Object) – It takes the index value as the input which needs to be updated with the new element

Example-

import java.util.ArrayList;

public class Test {

	public static void main(String args[]) {
		ArrayList<String> arayElements = new ArrayList<>();

		arayElements.add("QA");
		arayElements.add("TechHub");
		arayElements.add(0, "Welcome");
		System.out.println("Before Updating any element" + arayElements);

		arayElements.set(0, "Learn From");

		System.out.println("After updating element" + arayElements);
	}
}

Output-

Before removing any element[Welcome, QA, TechHub]
After removing element[Learn From, QA, TechHub]

Some of the additional methods provided for the Array List Manipulation are:

Method NameSummary
boolean addAll(Collection<? extends E> c)Appends all the element at the end of the list specified in the collection.
boolean addAll(int index, Collection<? extends E> c)Appends all the element at the specified location in the list provided in the collection.
void clear()Removes all the element from the list
void ensureCapacity(int requiredCapacity)To enhance capacity of an ArrayList instance.
E get(int index)To fetch an element from a particular position of list.
boolean isEmpty()Checks if the list is empty, then return a value, either true or false.
int lastIndexOf(Object o)Returns the last occurrence index value of the Object mentioned in the method
Object[] toArray()Converts the List to the array and returns the same.
Object clone()Returns a clone of the ArrayList, i.e., a duplicate copy
boolean contains(Object o)Checks if the element exists in the list, if yes then returns true, else false
int indexOf(Object o)Returns the index of the element mentioned, if element doesn’t exist then returns -1
E remove(int index)Removes the elements specified at the index position
boolean remove(Object o)Removes the first occurrence of the element specified.
boolean removeAll(Collection<?> c)Remove all the elements from the list
boolean removeIf(Predicate<? super E> filter)Remove the element from the list if the predicate mentioned is true
protected void removeRange(int fromIndex, int toIndex)Removes all the element from the list which fall under the start and the end index.
void replaceAll(UnaryOperator<E> operator)Replaces all the elements from the list with the specified element.
void retainAll(Collection<?> c)Retains all the elements mentioned in the list.
void sort(Comparator<? super E> c)Used to sort elements in the list on the basis of specified comparator.
List<E> subList(int fromIndex, int toIndex)Fetches all the elements that fall under the start index and end index
int size()Returns the size of the ArrayList

Linked List –

Linked List uses the concept of a doubly linked list. In an ArrayList we were able to work from one end only, but in a LinkedList we can perform manipulations from both the ends, i.e., the starting and the ending of the list.

It is provided under the java.util package. It is easier to handle and work with as compared to an ArrayList.

Some points about the Linked List, that we must take note of:

  • Uses Doubly linked list.
  • All the elements maintain the insertion order.
  • It can contain the duplicate elements.
  • It is non-synchronized.
  • Manipulations can be performed in a much faster way.

Syntax of Linked List

LinkedList<String> listofElements=new LinkedList<String>();

Manipulation that could be performed using LinkedList

Using the linked list, we can perform a lot of operations, some of which are discussed below:

  1. Adding Elements

With the Linked List we can add elements to a new or an existing Linked List. We is used to add() method to perform the add operation. It provides following methods:

  • add(Object) – It adds the element at the end of the List
  • add(int index, Object) – It adds the element at the particular index in the List.

Example-

import java.util.LinkedList;

public class Test {

	public static void main(String args[]) {
		LinkedList<String> arayElements = new LinkedList<>();

		arayElements.add("QA");
		arayElements.add("TechHub");

		System.out.println("Before adding new element" + arayElements);

		arayElements.add(0, "Welcome");

		System.out.println("After adding new element" + arayElements);
	}
}

Output-

Before adding new element[QA, TechHub]
After adding new element[Welcome, QA, TechHub]
  1. Removing Elements

We can remove the elements from the Linked List, by using remove() method. It provides the following methods :

  • remove(Object) – It removes the element from the LinkedList. If there are similar kinds of elements, then the first occurrence of that object is deleted.
  • remove(int index) – It takes the index value as the input, and whichever element is placed at that location is removed from the Linked List.

Example-

import java.util.LinkedList;

public class Test {

	public static void main(String args[]) {
		LinkedList<String> arayElements = new LinkedList<>();

		arayElements.add("QA");
		arayElements.add("TechHub");
		arayElements.add(0, "Welcome");
		System.out.println("Before removing any element" + arayElements);

		arayElements.remove("Welcome");

		System.out.println("After removing element" + arayElements);
	}
}

Output-

Before removing any element[Welcome, QA, TechHub]
After removing element[QA, TechHub]
  1. Update Elements

Once we have added the elements to the Linked List, we may want to update the elements. We can do the same by using the set () method.

 Following is the method provided for it:

  • set(int index, Object) – It takes the index value as the input which needs to be updated with the new element

Example-

import java.util.LinkedList;

public class Test {

	public static void main(String args[]) {
		LinkedList<String> arayElements = new LinkedList<>();

		arayElements.add("QA");
		arayElements.add("TechHub");
		arayElements.add(0, "Welcome");
		System.out.println("Before Updating any element" + arayElements);

		arayElements.set(0, "Learn From");

		System.out.println("After updating element" + arayElements);
	}
}

Output-

Before removing any element[Welcome, QA, TechHub]
After removing element[Learn From, QA, TechHub]

Some of the additional methods provided for the Linked List Manipulations are:

MethodDescription
boolean add(E e)Adds the mentioned element at the end of the list
void add(int index, E element)Adds the element at the specified index
boolean addAll(Collection<? extends E> c)Appends all the elements provided in the collection at the end of the list
void addFirst(E e)Adds the element at the beginning of the list
void addLast(E e)Adds the element at the end of the list
void clear()It clears all the elements from the list
Object clone()Returns a clone of the Linked List, i.e., a duplicate copy
boolean contains(Object o)Checks if the element exists in the list, if yes then returns true, else false
E element()Used to retrieve the first element of the Linked list
E get(int index)It is used to return the element at the specified position in a list.
E getFirst()Used to return the first element of the list
E getLast()Used to return the last element of the list
int indexOf(Object o)Returns the index of the element mentioned, if element doesn’t exist then returns -1
int lastIndexOf(Object o)Returns the last index  occurrence of the element mentioned, if element doesn’t exist then returns -1
ListIterator<E> listIterator(int index)Used to return a list-iterator of the elements in proper sequence.
boolean offer(E e)Adds the element at the last of the list
boolean offerFirst(E e)Inserts the element at the front
boolean offerLast(E e)Inserts the element at the end
E peek()Fetches the first element of the list
E peekFirst()Fetches the first element of the list or returns null if the list is empty
E peekLast()Fetches the last element of the list or returns null if the list is empty
E poll()Fetches and deletes the first element of the list
E pollFirst()Fetches and deletes the first element of the list, or returns null if the list is empty
E pollLast()Fetches and deletes the last element of the list, or returns null if the list is empty
E pop()Pops out the element from the stack representation of the list
void push(E e)Pushes in the element from the stack representation of the list
E remove()Removes the elements from the list
E remove(int index)Removes the elements specified at the index position
boolean remove(Object o)Removes the first occurrence of the element specified
Object[] toArray()Converts the List to the array and returns the same
int size()Returns the size of the ArrayList

Differences between ArrayList & LinkedList

BasisArrayListLinkedList
Internal WorkingUses Dynamic array to store the objects.Uses Doubly Linked List to store the objects.
PerformanceIt is slower in nature.It is faster in nature.
Used forBest used for storing and accessing the data.Best used for manipulating the data.
Tejasvi Nanda

About the Author

Tejasvi Nanda