hashmap of hashmap in java code example

Example 1: java hashmap syntax

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

	// First Example
    // Create a HashMap object called capitalCities
    HashMap capitalCities = new HashMap();

    // 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 stGrade = new HashMap(); 

	// 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

Example 2: hashmaps java

import java.util.HashMap;
//Within a class
//You can do new HashMap();, but you don't need to
HashMap examplehashmap=new HashMap<>();
{
//put in values
 examplehashmap.put(5, "example");
};
//get value
examplehashmap.get(5);
//returns "example"

Example 3: java hashmap example

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

Example 4: HashMap> hashMap = new HashMap<>();

HashMap> hashMap = new HashMap<>();

Tags:

Misc Example