LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Find the Error · Question 3

Error: calling add on a Map Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Double> prices = new HashMap<>();
        prices.add("tea", 20.0);
        System.out.println(prices);
    }
}
  • A 20.0 must be a float
  • B Map has no add method; entries are inserted with put(key, value) Correct
  • C prices must be a TreeMap
  • D Double cannot be a value
Correct Answer: Option B

add belongs to Collection, which Map does not extend.

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