Java10MapWithLocalVer.java
package com.java9r;
import java.util.HashMap;
public class Java10MapWithLocalVer {
public static void main(String[] args) {
var products = new HashMap<String, Object>();
products.put("Id", "100");
products.put("Name", "Product 1");
products.put("Price", 25.5);
products.put("Quantity", 75);
System.out.println("Product Details using foreach loop");
System.out.println("======================================");
products.forEach((key, value) -> {
System.out.println(key + ": " + value);
});
System.out.println("Iterate map using Enhanced for loop (keyset)");
System.out.println("======================================");
// Map<String, Object> products =new HashMap<String, Object>();
for (var key : products.keySet()) {
// String value = products.get(key);
var value = products.get(key);
System.out.println(key + ": " + value);
}
System.out.println("Iterate map using Enhanced for loop (entrySet)");
System.out.println("======================================");
// Map<String, Object> products = new HashMap<String, Object>();
// for (Map.Entry<String, Object> entry : map.entrySet()) {
for (var entry : products.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("Iterate map using while loop");
System.out.println("======================================");
// Map<String, Object> products = new HashMap<String, Object>());
// Iterator<Map.Entry<Integer, Integer>> entries =
// products.entrySet().iterator();
var entries = products.entrySet().iterator();
while (entries.hasNext()) {
// Map.Entry<Integer, Integer> entry = entries.next();
var entry = entries.next();
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("Product Details Iterating over keys or values using For-Each loop");
System.out.println("======================================");
System.out.println("iterating over keys only");
// Map<String, Object> products = new HashMap<String, Object>();
// iterating over keys only
for (var key : products.keySet()) {
System.out.println("Key = " + key);
}
System.out.println("iterating over values only");
// iterating over values only
for (var value : products.values()) {
System.out.println("Value = " + value);
}
}
}
Output
Product Details using foreach loop
======================================
Price: 25.5
Quantity: 75
Id: 100
Name: Product 1
Iterate map using Enhanced for loop (keyset)
======================================
Price: 25.5
Quantity: 75
Id: 100
Name: Product 1
Iterate map using Enhanced for loop (entrySet)
======================================
Price: 25.5
Quantity: 75
Id: 100
Name: Product 1
Iterate map using while loop
======================================
Price: 25.5
Quantity: 75
Id: 100
Name: Product 1
Product Details Iterating over keys or values using For-Each loop
======================================
iterating over keys only
Key = Price
Key = Quantity
Key = Id
Key = Name
iterating over values only
Value = 25.5
Value = 75
Value = 100
Value = Product 1
0 comments:
Post a Comment