3

So I'm trying to get the key of a TreeMap by an int index.

Here's my code:

TreeMap<String, Integer> map = new TreeMap<String, Integer>(Collections.reverseOrder());
map.put("hi", 1);
map.put("hi2", 5);

System.out.println("Key: " + (String) map.keySet().toArray()[0] + "\nValue: " + map.get(0));

But when I execute this, I get the error:

java.lang.Integer cannot be cast to java.lang.String

So I'm trying to get the key: "hi2"

ClashClown
  • 63
  • 1
  • 7
  • Wait. *What* is it you're trying to do again? – Makoto Aug 10 '14 at 17:22
  • 1
    You must at least show us how your map is declared and instantiated. We don't anything about the type of your variables. Plus, your question seems weird to me. – Dici Aug 10 '14 at 17:23
  • If you need to get the element by its index (rather than its key), you need to consider the question whether `TreeMap` is the right data structure for your problem. – NPE Aug 10 '14 at 17:23
  • I'm trying to display the key located at the i index – ClashClown Aug 10 '14 at 17:23
  • Well if your key has type String you cannot do map.get(0). And of course, since the value is of type int you cannot cast it to String. You are interverting the types of your map. – Dici Aug 10 '14 at 17:27
  • 1
    A TreeMap uses a tree as its internal structure. Trees don't have indices. Arrays and lists have indices. – JB Nizet Aug 10 '14 at 17:30
  • So wait. If I understand you correctly, you want to extract the *key* of the map given a specific *value*, right? – Makoto Aug 10 '14 at 17:31

2 Answers2

5

The problem is

"\nValue: " + map.get(0)

The argument for get must be a String, not an Integer like 0. So do this:

String key = map.keySet().toArray()[0];
Integer value = map.get(key);
System.out.println("Key: " + key + "\nValue: " + value);
  • 1
    Thanks I got it! When I wrote this, I thought the get method got the value by it's index, but thanks works now! – ClashClown Aug 10 '14 at 17:30
  • It's obviously that. However, it makes me wonder why the get method takes an Object parameter and not a String. – Dici Aug 10 '14 at 17:30
  • What `Object` parameter? –  Aug 10 '14 at 17:31
  • The Map interface is declared as `Map` so I wonder why get is declared as `V get(Object)` and not `V get(K)`. But I guess a comment is not the right place for that. – Dici Aug 10 '14 at 17:34
  • @Dici: http://stackoverflow.com/questions/857420/what-are-the-reasons-why-map-getobject-key-is-not-fully-generic – JB Nizet Aug 10 '14 at 17:36
1

It seems to me that the error comes from the latter part of the println statement:

println(...map.get(0))

Where the integer 0 has a type conflict with the map, where the key type is String.

[edit]

  • As for the original question about getting the key by index, I think you did it correctly: map.keySet().toArray()[0]
  • For the error complaining about key type conflict in map.get(), you can correct the parameter type, as suggested by @Tichodroma's answer above.
  • Thanks @Tichodroma for the suggestion to improve the answer! I added two actionable suggestions. –  Aug 10 '14 at 17:47