Java LinkedList forEach() Method with Examples

In the previous article, we have discussed about Java LinkedList poll() Method with Examples

In this article we are going to see the use of Java LinkedList forEach() method along with suitable examples.

Java LinkedList forEach() Method with Examples

This java.util.LinkedList.forEach() method is used to iterate each element of the LinkedList and perform some action on each element one by one if required.

Syntax:

LinkedListName.forEach()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

Method-1: Java LinkedList forEach() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now, using the forEach() method we will perform some action for each element one by one.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an LinkedList of string datatype
        LinkedList<String> l1 = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l1.add("vivo");
        l1.add("htc");
        l1.add("samsung");
        l1.add("realme");
        l1.add("nokia");
        // Prints the LinkedList elements
        System.out.println("The elements in the LinkedList are: "+l1);
        // add 1 on each element with a space using forEach() method
        System.out.print("The new elements in the LinkedList are: ");
        l1.forEach((e) -> {System.out.print(e + " 1 ");});
    }
}

Output:

The elements in the LinkedList are: [vivo, htc, samsung, realme, nokia]
The new elements in the LinkedList are: vivo 1 htc 1 samsung 1 realme 1 nokia 1

Method-2: Java LinkedList forEach() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now, using the forEach() method we will perform some action for each element one by one.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {  
        // Create an LinkedList of Integer datatype
        LinkedList<Integer> l1 = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l1.add(1);
        l1.add(100);
        l1.add(84);
        l1.add(17);
        l1.add(0);
        // Prints the LinkedList elements
        System.out.println("The elements in the LinkedList are: "+l1);
        // add 1 on each element using forEach() method
        System.out.print("The new elements in the LinkedList are: ");
        l1.forEach((e) -> {System.out.print(e +1+" ");});
    }
}

Output:

The elements in the LinkedList are: [1, 100, 84, 17, 0]
The new elements in the LinkedList are: 2 101 85 18 1

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: