Keatinge
0
Q:

how to create a hashmap in java

// Import the HashMap class
import java.util.HashMap;

    // Create a HashMap object called capitalCities
    HashMap<String, String> capitalCities = new HashMap<String, String>();

    // Add keys and values (Country, City)
    capitalCities.put("England", "London");
    capitalCities.put("Germany", "Berlin");
    capitalCities.put("Norway", "Oslo");
    capitalCities.put("USA", "Washington DC");
    System.out.println(capitalCities);
17
//Hash map creation
Map< String,Integer> hm =  
                        new HashMap< String,Integer>(); 
//inserting elements into hashmap
       hm.put("a", new Integer(100)); 
       hm.put("b", new Integer(200)); 
       hm.put("c", new Integer(300)); 
       hm.put("d", new Integer(400)); 
4
//remember to first import java.util.*; first

//you can swap out string or integer for other data types
Map<String, Integer> d = new HashMap<>();
0
HashMap<Integer,String> map=new HashMap<Integer,String>();//key is integer, value is String
2
import java.util.HashMap; // import the HashMap class

HashMap<String, String> capitalCities = new HashMap<String, String>();
3

New to Communities?

Join the community