Codes
Answers
// 2 Iterating over entrySet of map
Map<String, Integer> cityPINsMap = new HashMap<>();
cityPINsMap.put("NewDelhi", 110001);
cityPINsMap.put("NewYork", 10001);
cityPINsMap.put("Sydney", 2000);
for (Map.Entry<String, Integer> entry : cityPINsMap.entrySet()) {
System.out.println("City = " + entry.getKey() + ", PIN = " + entry.getValue());
}
//4. Iterating through forEach
Map<Integer, String> countriesMap = new HashMap<>();
countriesMap.put(1, "India");
countriesMap.put(2, "USA");
countriesMap.put(3, "Australia");
countriesMap.forEach((k, v) -> System.out.println("No = " + k + ", Country = " + v));
//5. Iterating through forEach java 8
Map<Integer, String> contentsMap = new HashMap<>();
contentsMap.put(1, "India");
contentsMap.put(2, "USA");
contentsMap.put(3, "Australia");
contentsMap.entrySet().stream().forEach(e ->
System.out.println("No : " + e.getKey() + " Content : " + e.getValue())
);
// 2 Iterating over entrySet of map
Map<String, Integer> cityPINsMap = new HashMap<>();
cityPINsMap.put("NewDelhi", 110001);
cityPINsMap.put("NewYork", 10001);
cityPINsMap.put("Sydney", 2000);
for (Map.Entry<String, Integer> entry : cityPINsMap.entrySet()) {
System.out.println("City = " + entry.getKey() + ", PIN = " + entry.getValue());
}
//4. Iterating Map through forEach
Map<Integer, String> countriesMap = new HashMap<>();
countriesMap.put(1, "India");
countriesMap.put(2, "USA");
countriesMap.put(3, "Australia");
countriesMap.forEach((k, v) -> System.out.println("No = " + k + ", Country = " + v));
Questions
Answers
Answer accepted
Users
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us