Java Learner logo
← Back to all exercises

Maps

Keeping insertion order

Fill in the two lines that this LinkedHashMap iteration will print.

Maps

Keeping insertion order

IntermediateAwaiting attempt

Fill in the two lines that this LinkedHashMap iteration will print.

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> missions = new LinkedHashMap<>();
        missions.put("Mercury", 1959);
        missions.put("Gemini", 1964);
        missions.put("Mercury", 1961);
        missions.putIfAbsent("Apollo", 1967);
        missions.remove("Gemini");

        for (var entry : missions.entrySet()) {
            System.out.println(entry.getKey() + " -> " + entry.getValue());
        }
    }
}