“Java iterate melalui peta” Kode Jawaban

Java iterate melalui peta

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

Iterasi melalui peta 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

Melintasi melalui peta java 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());
		}
	}
}
Outrageous Ostrich

Jawaban yang mirip dengan “Java iterate melalui peta”

Pertanyaan yang mirip dengan “Java iterate melalui peta”

Lebih banyak jawaban terkait untuk “Java iterate melalui peta” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya