How do I insert elements at a specific index in Java list?



In this article, we will learn how to insert an element at a specific index in a Java list. For example, we have a fruit list by certain order and we want to insert a new fruit at a specific index in the list.

Following are the ways to insert an element at a specific index in a Java list:

Using add() Method

The add() method of the the list interface helps us to insert an element at whichever index we want. We can specify the index at which we want to insert the element as the first argument and the element to be inserted as the second argument.

Syntax

Following is the syntax of the add() method. First argument is index and the second is the element to be inserted.

list.add(index, element);

Example

In the following example, we will create a list of fruits and then insert a new fruit at a specific index using the add() method.

import java.util.ArrayList;
import java.util.List;
public class InsertAtSpecificInd{
   public static void main(String[] args){
      List<String> fruits = new ArrayList<>();
      fruits.add("Apple");
      fruits.add("Banana");
      fruits.add("Cherry");
      fruits.add("Date");
      System.out.println("Fruits List before insertion: " + fruits);

      System.out.println("Inserting 'Mango' at index 2");
      fruits.add(2, "Mango");
      System.out.println("Fruits List after insertion: " + fruits);
      System.out.println("Inserting 'Orange' at index 0");
      fruits.add(0, "Orange");
      System.out.println("Fruits List after insertion: " + fruits);
   }
}

Output

Following is the output of the above code:

Fruits List before insertion: [Apple, Banana, Cherry, Date]
Inserting 'Mango' at index 2
Fruits List after insertion: [Apple, Banana, Mango, Cherry, Date]
Inserting 'Orange' at index 0
Fruits List after insertion: [Orange, Apple, Banana, Mango, Cherry, Date]

Using ListIterator

ListIterator is used for traversing a list from both the directions. We will use the method add() of the ListIterator to insert an element at a specific index.

Syntax

Following is the syntax of the add() method of ListIterator. First argument is the element to be inserted.

listIterator.add(element);

Example

In the following example, we will create a list of fruits and then insert a new fruit at a specific index using the ListIterator.

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class InsertAtSpecificInd {
   public static void main(String[] args) {
      List<String> fruits = new ArrayList<>();
      fruits.add("Apple");
      fruits.add("Banana");
      fruits.add("Cherry");
      fruits.add("Date");
      System.out.println("Fruits List before insertion: " + fruits);

      ListIterator<String> iterator = fruits.listIterator();
      
      // Move to index 2
      while (iterator.hasNext() && iterator.nextIndex() < 2) {
         iterator.next();
      }
      
      System.out.println("Inserting 'Mango' at index 2");
      iterator.add("Mango");
      System.out.println("Fruits List after insertion: " + fruits);
   }
}

Output

Following is the output of the above code:

Fruits List before insertion: [Apple, Banana, Cherry, Date]
Inserting 'Mango' at index 2
Fruits List after insertion: [Apple, Banana, Mango, Cherry, Date]

Using Stream API

Java Stream API provides a way to process sequences of elements. We can use the Stream API to insert an element at a specific index in a list.

We will use the Stream.concat() method to concatenate the elements before and after the specified index with the new element.

Syntax

Following is the syntax of the Stream API to insert an element at a specific index:

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class InsertAtSpecificInd {
   public static void main(String[] args) {
      List<String> fruits = List.of("Apple", "Banana", "Cherry", "Date");
      System.out.println("Fruits List before insertion: " + fruits);

      String newFruit = "Mango";
      int index = 2;

      List<String> updatedFruits = Stream.concat(
         fruits.stream().limit(index),
         Stream.concat(Stream.of(newFruit), fruits.stream().skip(index))
      ).collect(Collectors.toList());

      System.out.println("Fruits List after insertion: " + updatedFruits);
   }
}

Output

Following is the output of the above code:

Fruits List before insertion: [Apple, Banana, Cherry, Date]
Fruits List after insertion: [Apple, Banana, Mango, Cherry, Date]
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-17T19:31:42+05:30

633 Views

Kickstart Your Career

Get certified by completing the course

Get Started