add methods (#11579)

This commit is contained in:
Kai Yuan 2021-12-19 01:56:28 +01:00 committed by GitHub
parent 6b6f1fcf1a
commit dd9dcae1f4
1 changed files with 44 additions and 0 deletions

View File

@ -17,22 +17,45 @@ public class MapIteration {
System.out.println("Iterating Keys of Map Using KeySet");
mapIteration.iterateKeys(map);
System.out.println("Iterating Values of Map Using values()");
mapIteration.iterateValues(map);
System.out.println("Iterating Map Using Entry Set");
mapIteration.iterateUsingEntrySet(map);
System.out.println("Iterating Using Iterator and Map Entry");
mapIteration.iterateUsingIteratorAndEntry(map);
System.out.println("Iterating Using Iterator and KeySet");
mapIteration.iterateUsingIteratorAndKeySet(map);
System.out.println("Iterating values Using Iterator and values()");
mapIteration.iterateUsingIteratorAndValues(map);
System.out.println("Iterating Using KeySet and For Each");
mapIteration.iterateUsingKeySetAndForeach(map);
System.out.println("Iterating Map Using Lambda Expression");
mapIteration.iterateUsingLambda(map);
System.out.println("Iterating Map By Keys Using Lambda Expression");
mapIteration.iterateByKeysUsingLambda(map);
System.out.println("Iterating values Using Lambda Expression");
mapIteration.iterateValuesUsingLambda(map);
System.out.println("Iterating Using Stream API");
mapIteration.iterateUsingStreamAPI(map);
}
public void iterateUsingIteratorAndValues(Map<String, Integer> map) {
Iterator<Integer> iterator = map.values().iterator();
while (iterator.hasNext()) {
Integer value = iterator.next();
System.out.println("value :" + value);
}
}
public void iterateUsingEntrySet(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
@ -43,6 +66,14 @@ public class MapIteration {
map.forEach((k, v) -> System.out.println((k + ":" + v)));
}
public void iterateByKeysUsingLambda(Map<String, Integer> map) {
map.keySet().forEach(k -> System.out.println((k + ":" + map.get(k))));
}
public void iterateValuesUsingLambda(Map<String, Integer> map) {
map.values().forEach(v -> System.out.println(("value: " + v)));
}
public void iterateUsingIteratorAndEntry(Map<String, Integer> map) {
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet()
.iterator();
@ -52,6 +83,14 @@ public class MapIteration {
}
}
public void iterateUsingIteratorAndKeySet(Map<String, Integer> map) {
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + ":" + map.get(key));
}
}
public void iterateUsingKeySetAndForeach(Map<String, Integer> map) {
for (String key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
@ -68,7 +107,12 @@ public class MapIteration {
for (String key : map.keySet()) {
System.out.println(key);
}
}
public void iterateValues(Map<String, Integer> map) {
for (Integer value : map.values()) {
System.out.println(value);
}
}
}