Java ListIterator remove() Method Tutorial

In this section, we will learn what the ListIterator remove() method is and how to use it in Java.

What is ListIterator remove() Method in Java?

The Java ListIterator remove() method is used to remove the current element of a collection list that was returned as a result of calling either the next() or previous() method.

Note: you can only call this method once per each call to the next() or previous() method.

Also, you can only call the method if a call to the `add()` method didn’t occur.

ListIterator remove() Method Syntax:

void remove()

ListIterator remove() Method Parameters

The method does not take an argument.

ListIterator remove() Method Return Value

The return value of this method is void.

ListIterator remove() Method Exception:

The method might throw two types of exceptions:

UnsupportedOperationException: If the remove() method is not supported in the target ListIterator object, we will get this exception.

IllegalStateException: If neither the next() or previous() method has been called or the add() method was called before calling the remove() method, we will get this exception.

Example: using ListIterator remove() Method in Java

import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
public class Main {

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

        ListIterator<String> iterate = list.listIterator();

         System.out.println("The size of the list is: "+ list.size());


The size of the list is: 0

How Does ListIterator remove() Method Work in Java?

In this example, because we’ve called the remove() method on each returned value from the list, the entire element got removed from the list and, as a result, its size turned 0.


Top Technologies