RWest
9
Q:

java hashmap example

// 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
import java.util.HashMap;
//Within a class
//You can do new HashMap<Key Type, Value Type>();, but you don't need to
HashMap<Int, String> examplehashmap=new HashMap<>();
{
//put in values
 examplehashmap.put(5, "example");
};
//get value
examplehashmap.get(5);
//returns "example"
7
//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
// Import the HashMap class
import java.util.HashMap;

	// First Example
    // 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);

	//Second Example
	// Create a HashMap object called stGrade
	Map<String, Integer> stGrade = new HashMap<String, Integer>(); 

	// Insert elements 
       	stGrade.put("aaron", new Integer(90)); 
       	stGrade.put("isaac", new Integer(100)); 
       	stGrade.put("john", new Integer(35)); 
       	stGrade.put("mohammad", new Integer(100)); 

	// Get value
	   	stGrade.get("mohammad"); 	// returns 100
		stGrade.get("aaron"); 		// returns 90
		stGrade.get("john");	 	// returns 35
		stGrade.get("isaac"); 		// returns 100
		
3
import java.util.HashMap;

HashMap<String, Integer> key = new HashMap<String, Integer>();
key.put("abcd", 5);

System.out.println(key.get(5)); //prints out abcd
2
HashMap<Integer,String> map=new HashMap<Integer,String>();//key is integer, value is String
2
import java.util.Map;
import java.util.HashMap;

Map<String, Integer> key = new HashMap<>();
key.put("addedVal", 245);

System.out.println("" + key.get("addedVal")); //prints out 245
System.out.println("" + key.getOrDefault("addedVal", 0)); //prints out 245, key could be found
System.out.println("" + key.getOrDefault("asff", 0)); //prints out 0, no given key found in map
1
import java.util.HashMap; // import the HashMap class

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

New to Communities?

Join the community