LinkedHashMap forEach(BiConsumer) in Java

forEach(BiConsumer): This method is available in java.util.LinkedHashMap class of Java.

Syntax:

void java.util.LinkedHashMap.forEach(BiConsumer<? super K, ? super V> action)

This method takes one argument. This method performs the given action for each entry in this map until all entries have been processed or the action throws an exception.

Parameters: One parameter is required for this method.

action: The action to be performed for each entry.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.LinkedHashMap;

public class LinkedHashMapforEach {

    public static void main(String args[]) {

        LinkedHashMap<String, Integer> linkedHashMap =
new LinkedHashMap<String, Integer>();

        linkedHashMap.put("Java", 1);
        linkedHashMap.put("Hello", 2);

        linkedHashMap.forEach(((k, v) ->
System.out.println(k + "==" + v)));

    }

}

Output:

Java==1

Hello==2


No comments:

Post a Comment