LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Predict the Output · Question 13

Removing entries with entrySet().removeIf Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> marks = new TreeMap<>(Map.of("Asha", 91, "Ravi", 35, "Meena", 78, "John", 28, "Zoya", 40));
        marks.entrySet().removeIf(e -> e.getValue() < 40);
        marks.values().removeIf(v -> v == 40);
        marks.keySet().remove("Meena");
        System.out.println(marks);
    }
}
  • A {Asha=91} Correct
  • B {Asha=91, Meena=78}
  • C {Asha=91, Meena=78, Zoya=40}
  • D ConcurrentModificationException
Correct Answer: Option A

entrySet(), values() and keySet() are live views; removing through them removes from the map safely.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links