Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Traversing through java map foreach

// Java Program to Demonstrate
// Working of Map interface

// Importing required classes
import java.util.*;

// Main class
class Main {

	// Main driver method
	public static void main(String args[])
	{
		// Creating an empty HashMap
		Map<String, Integer> hashmap= new HashMap<String, Integer>();

		// Inserting pairs in above Map
		// using put() method
		hashmap.put("Banana", new Integer(100));
		hashmap.put("Orange", new Integer(200));
		hashmap.put("Mango", new Integer(300));
		hashmap.put("Apple", new Integer(400));

		// Traversing through Map using for-each loop
		for (Map.Entry<String, Integer> map :
			hashmap.entrySet()) {

			// Printing keys
			System.out.print(map.getKey() + ":");
			System.out.println(map.getValue());
		}
	}
}
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Traversing #java #map #foreach
ADD COMMENT
Topic
Name
1+7 =