25.0k views
3 votes
Write a program to store the names of 10 countries and their capitals in HashMap, where the name of the country is the key and its capital is the value. Assume that each country only has one capital.

User Gaudy
by
4.5k points

1 Answer

5 votes

Answer:

package one;

import java.util.*;

public class One {

public static void main(String[] arr) {

/* Create object of HashMap */

HashMap<String, String> obHashMap = new HashMap<String, String>();

/* Store value in HashMap */

obHashMap.put("Bhutan", "Thimphu ");

obHashMap.put("China", "Beijing ");

obHashMap.put("India", "New Delhi ");

obHashMap.put("Israel", "Jerusalem ");

obHashMap.put("Japan", "Tokyo ");

/* Create a set of keys of hashmap */

System.out.print("COUNTRY"+ ": ");

System.out.println("CAPITAL");

Set set=obHashMap.entrySet();

Iterator obIter = set.iterator();

while (obIter.hasNext()) {

Map.Entry me=(Map.Entry)obIter.next();

System.out.print(me.getKey() + ": ");

System.out.println(me.getValue());

}

}

}

User Adam Lavin
by
4.0k points