L11.8: Maps Keys, Values in Different Types

PHOTO EMBED

Thu Oct 19 2023 01:59:19 GMT+0000 (Coordinated Universal Time)

Saved by @testpro #java

import java.util.HashMap;

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

        // {key:value, key2:value2 ...}

        // France: Paris
        // Italy: Rome
        // Norway: Oslo
        // US: Washington DC

        HashMap<String, String> countryCapitals = new HashMap<>();
        countryCapitals.put("France","Paris");
        countryCapitals.put("Italy", "Rome");
        countryCapitals.put("Norway", "Oslo");
        countryCapitals.put("US", "Washington DC");
        countryCapitals.remove("Italy");

        HashMap<Integer, String> capitals = new HashMap<>();
        capitals.put(1,"Paris");
        capitals.put(2, "Rome");
        capitals.put(3, "Oslo");
        capitals.put(4, "Washington DC");

        System.out.println(capitals.get(3));
    }
}
// Output: Oslo
content_copyCOPY