“Java iterate através do mapa” Respostas de código

Java iterate através do mapa

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

Iterando através do mapa Java

// 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);
		}
	}
}
Outrageous Ostrich

Atravessando o mapa Java para e cada vez

// 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());
		}
	}
}
Outrageous Ostrich

Respostas semelhantes a “Java iterate através do mapa”

Perguntas semelhantes a “Java iterate através do mapa”

Mais respostas relacionadas para “Java iterate através do mapa” em Java

Procure respostas de código populares por idioma

Procurar outros idiomas de código