Java Code Example: HashMap alphabet

This Java code creates a HashMap with key-value pairs of type that map letters of the alphabet to their position in the alphabet. It then uses a forEach loop with a lambda expression to print out each key-value pair in the map.

import java.util.HashMap;

public class Cube {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("A", "1");
        map.put("B", "2");
        map.put("G", "7");
        map.put("M", "13");

        map.forEach((key, value) -> {
            System.out.printf("%s is the %s letter in alphabet\n", key, value);
        });
    }
}
Output
A is the 1 letter in alphabet
B is the 2 letter in alphabet
G is the 7 letter in alphabet
M is the 13 letter in alphabet