BAEL-4719 - Using the Map.Entry Java Class (#10428)
* BAEL-4719 Using the Map.Entry Java Class * BAEL-4719 Using the Map.Entry Java Class * BAEL-4719 Change description * BAEL-4719 Feedback from first draft
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.map.entry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapEntryEfficiencyExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
MapEntryEfficiencyExample mapEntryEfficiencyExample = new MapEntryEfficiencyExample();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
map.put("Robert C. Martin", "Clean Code");
|
||||
map.put("Joshua Bloch", "Effective Java");
|
||||
|
||||
System.out.println("Iterating Using Map.KeySet - 2 operations");
|
||||
mapEntryEfficiencyExample.usingKeySet(map);
|
||||
|
||||
System.out.println("Iterating Using Map.Entry - 1 operation");
|
||||
mapEntryEfficiencyExample.usingEntrySet(map);
|
||||
|
||||
}
|
||||
|
||||
public void usingKeySet(Map<String, String> bookMap) {
|
||||
for (String key : bookMap.keySet()) {
|
||||
System.out.println("key: " + key + " value: " + bookMap.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public void usingEntrySet(Map<String, String> bookMap) {
|
||||
for (Map.Entry<String, String> book: bookMap.entrySet()) {
|
||||
System.out.println("key: " + book.getKey() + " value: " + book.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user