Use Index With forEach in Java

MD Aminul Islam Sep 28, 2022
  1. Use the forEach() Method With an Array Index
  2. Use the forEach() Method With a List and HashMap Index
Use Index With forEach in Java

You can follow this Java article if you want to use the Java forEach() method with a specific index. Sometimes, we must perform a particular task related to a specific index.

In this article, we will learn how to use the forEach() function with the combination of indexes. Also, we will cover the topic by using necessary examples and explanations to make the topic easier.

In general, the forEach() method doesn’t allow us to use an index with it, but there are some ways to do this. For this purpose, we must use the IntStream.range() method.

Let’s take a look at an example.

Use the forEach() Method With an Array Index

In our below example, we will demonstrate how we can use the forEach() method with a specific index of an array. The code for our example is provided below:

// importing necessary packages
import java.util.stream.IntStream;

public class JavaForEachIndex {
    public static void main(String[] args) {

        // Creating an array of string
        String[] StrArr = { "A", "B", "C", "D", "E" };

        // Finding the length of the string array
        int Len = StrArr.length;

        // Using forEach with index
        IntStream.range(0, Len)
                .forEach(index -> System.out.println("Value of array at Index : " + (index + 1) + " = " + StrArr[index]));
    }
}

We already commanded each line’s purpose. Now, after running the example code, you will see the below output:

Value of array at Index : 1 = A
Value of array at Index : 2 = B
Value of array at Index : 3 = C
Value of array at Index : 4 = D
Value of array at Index : 5 = E

Use the forEach() Method With a List and HashMap Index

In our below example, we will illustrate how we can use the forEach() method with a specific list index. The code for our example is the one below:

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class JavaForEachIndex {    public static void main(String[] args) {

    // Creating a list of string
    List<String> StrList = Arrays.asList("A", "B", "C", "D", "E");

    // Creating a HashMap
    // Put the List of String to the HashMap
    HashMap<Integer, String> Collection = StrList.stream().collect(HashMap<Integer, String>::new,
                    (MyMap, StreamVal) -> MyMap.put(MyMap.size(), StreamVal),(MyMap, map2) -> {});

    // Using the forEach with index
    Collection.forEach((index, val) -> System.out.println("Value of list element at "+ index + " = " + val));
}
}

We already commanded each line’s purpose. Now, after running the example code, you will see the below output in your console:

Value of list element at 0 = A
Value of list element at 1 = B
Value of list element at 2 = C
Value of list element at 3 = D
Value of list element at 4 = E

Please note that the code examples shared here are in Java, and you must install Java on your environment if your system doesn’t contain Java.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Java Loop