LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Find the Error · Question 7

Error: primitive types as map type arguments Answer, explanation & discussion

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

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

Main.java
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<int, double> rates = new TreeMap<>();
        rates.put(1, 6.5);
        System.out.println(rates);
    }
}
  • A TreeMap cannot store numbers
  • B Type arguments must be reference types: use TreeMap<Integer, Double>, and autoboxing converts the values Correct
  • C rates needs a Comparator
  • D 6.5 must be 6.5f
Correct Answer: Option B

javac: "unexpected type – required: reference, found: int".

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