Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java iterate through map

for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
}
Comment

Iterating Through the Java Map

// Java program to demonstrate
// the working of Map interface

import java.util.*;
class Main {
	public static void main(String args[])
	{

		// Initialization of a Map
		// using Generics
		Map<Integer, String> hashmap1= new HashMap<Integer, String>();

		// Inserting the Elements
		hashmap1.put(1, "Apple");
		hashmap1.put(2, "Banana");
		hashmap1.put(3, "Mango");


		for (Map.Entry mapElement : hashmap1.entrySet()) {
			int key= (int)mapElement.getKey();

			// Finding the value
			String value= (String)mapElement.getValue();
			System.out.println(key + " : "+ value);
		}
	}
}
Comment

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());
		}
	}
}
Comment

Iterating through a map

for (auto const& [key, value] : your_map) {
  std::cout << key    << ':'    << value    << std::endl;
}
Comment

PREVIOUS NEXT
Code Example
Java :: difference between java and javax 
Java :: navigate from one fragment to another android 
Java :: java array to list 
Java :: sorting hashmap by key in java 8 
Java :: jagged array java 
Java :: char equals java 
Java :: install java 8 on windows 10 
Java :: android.permission.INTERNET 
Java :: java stack using linked list 
Java :: how to print multiple lines in java 
Java :: capitalize string java 
Java :: What is null mean in java 
Java :: java filedialog 
Java :: what is the use of println 
Java :: java transfer file over socket 
Java :: how to wait in java 
Java :: try catch java 
Java :: java string to double 
Java :: @data lombok 
Java :: declare array with values java 
Java :: sorting an arraylist 
Java :: array liste java 
Java :: android java get current time 
Java :: how to read file from assets folder in android 
Java :: share intent android 
Java :: action bar message spigot 1.8 
Java :: AmazonS3ClientBuilder 
Java :: java arithmetic operators 
Java :: math library java 
Java :: how to summon lightning in minecraft 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =