Home > Article >

Article: Add, Get, and Remove an Element from a Java List

A list in Java is no more than an ordered collecion.

Published

java list
Author: Mensah Alkebu-Lan

Table of Contents #

Introduction #

When getting started with what we call Java’s collections, a list is a good place to start. A list in Java is no more than an ordered collection. Unlike sets in Java, which we will discuss in another article, lists in Java allow duplicate elements.

List’s add Method #

Java’s List interface extends its Collection interface. One of the methods it inherits from this Collection interface is its add method. When talking about the Java’s List interface, this is a good place to start.

You’ll notice List has two add methods, while the Collection interface has one. In other words, List’s add method has two signatures.

The simpler signature it inherits from the Collection interface takes an element matching the type of object designated by the List and appends it to the end of the List. The method returns true if the method executes without incident.

The second signature of List’s add method takes not only the element to be added but also the index where the element should be added. This operation will shift the element currently at that position and any subsequent elements one position to the “right” This method has void return type.

Below is the syntax of the two methods:


boolean add(E e)
void add(int index, E element)

In order to get our hands dirty with lists in Java, we’re going to have to below the List interface to one of its implementation classes. A good list class to start with is the ArrayList class.

The code snippet below creates an instance of ArrayList:

       List<Employee> list = new ArrayList<>();

Further, the code snippet below captures examples of each add method:


package com.uequations.list;

import com.uequations.model.Employee;

import java.util.ArrayList;
import java.util.List;

public class ListDemo {

public List<Employee> demoAddToList() {

Employee e1 = new Employee("Happy", "Golukcy",1);
Employee e2 = new Employee("Mensah", "Alkebu-Lan",2);
Employee e3 = new Employee("Beyonce", "Knowles-Carter",3);

List<Employee> list = new ArrayList<>();

list.add(e1);
list.add(e2);
list.add(1,e3);

return list;
}
}

Unit Testing our First Method #

Now let’s test this to see how this works. To do so, let’s introduce the List interface’s get method. It’s a simple method taking the integer position of an element as a parameter and returning the element at that position.

Below is the syntax of the get method:


E get(int index)

With that being said, I think we’re ready to test this.

The code snippet below captures a unit test for our demoAddToList method:


package com.uequations.list;

import com.uequations.model.Employee;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class ListDemoTest {

@Test
public void testDemoAddToList() {

List<Employee> employeeList = new ListDemo().demoAddToList();

assertEquals(employeeList.get(0).getFirstName(), "Happy");
assertEquals(employeeList.get(0).getFirstName(), "Happy");
assertEquals(employeeList.get(1).getFirstName(), "Beyonce");
}

}

From the following unit test, you can deduce some of the behavior of our demoAddToList method. You’ll notice the first element of the list is at the 0 position. You’ll also notice executing the get method on a list twice with the same integer position parameter will return the same result. Lastly, you will also notice the second add method placed the Beyonce employee at the second or 1 position, meaning the Mensah employee is at the last or 2 position.

List’s remove Method #

A logical place to go next would be its remove methods. Similar to its add methods, only one of List’s remove methods is inherited from the Collection interface. This remove method takes an object and returns a boolean. The List interface’s other remove method takes an integer value representing the position of the element removed.

Below is syntax of the two method’s signatures:


E remove(int index)

boolean remove(Object o)

The code snippet below captures an example of each remove method:



public List<Employee> demoRemoveFromList() {

List<Employee> list = demoAddToList();

int i = 0;
Employee j = new Employee("Beyonce", "Knowles-Carter",3);

list.remove(i);
list.remove(j);

return list;
}

Our above implementation assumes our Employee class overrides the equals and hashCode methods. Otherwise, when the remove method is executed, no element will be removed.

The code snippet below is an example:

package com.uequations.model;

import java.util.Objects;

public class Employee {



@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(getFirstName(), employee.getFirstName()) && Objects.equals(getLastName(), employee.getLastName()) && Objects.equals(getEmployeeId(), employee.getEmployeeId());
}

@Override
public int hashCode() {
return Objects.hash(getFirstName(), getLastName(), getEmployeeId());
}
}

With that being said, I think we’re ready to unit test this method. The code snippet below captures a unit test for our demoRemoveFromList method:



@Test
public void testDemoRemoveFromList() {

List<Employee> employeeList = new ListDemo().demoRemoveFromList();

assertEquals("Mensah",employeeList.get(0).getFirstName());
assertEquals(1,employeeList.size());
}

You can find the source to this exercise on Github.

References #

List (Java Platform SE 8 ). https://docs.oracle.com/javase/8/docs/api/java/util/List.html. Accessed 12-8-2021.