1

I've created a TreeSet and I'm trying to add a Map.Entry to it. But this will not compile. What am I doing wrong?

TreeSet<Map.Entry<String, Integer>> treeSet = new TreeSet<Map.Entry<String, Integer>>();

treeSet.add( Map.Entry<String, Integer>("Text string...", 123) );
user1121487
  • 2,662
  • 8
  • 42
  • 63

2 Answers2

3

I don't know why you are trying to instantiate a Map.Entry. I guess you can simply use a TreeMap.

Even though, if you want that, you can instantiate AbstractMap.SimpleEntry like this:

Map.Entry<String,Integer> entry =
    new AbstractMap.SimpleEntry<String, Integer>("exmpleString", 42);

Note that Map.Entry is an interface. Oh! And of course, as rightly specified in comments by @Louis, Map.Entry doesn't implement a Comparable, so you won't be able to add it to TreeSet.

You can however create your own implementation of this interface, make that implement Comparable<Map.Entry<K, V>>, and use that instead.


But as I said, you can use a TreeMap, and use its entrySet() method which will give you a Set<Map.Entry<K, V>>:

SortedMap<String, Integer> map = new TreeMap<>();
Set<Map.Entry<String, Integer>> set = map.entrySet();
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 1
    The only downside of this answer is the usage of `AbstractMap.SimpleEntry`. – Luiggi Mendoza Oct 09 '13 at 16:50
  • `Map.Entry` doesn't implement `Comparable`, so you can't add a `Map.Entry` to a `TreeSet` without specifying a `Comparator`. – Louis Wasserman Oct 09 '13 at 16:53
  • I must use TreeSet>, so if I do something like: http://stackoverflow.com/questions/3110547/java-how-to-create-new-entry-key-value it should work? – user1121487 Oct 09 '13 at 16:58
  • 3
    No, that's still not enough; you must implement a `Comparator>`. Frankly, it's a ridiculous requirement that you have to use a `TreeSet>`. – Louis Wasserman Oct 09 '13 at 17:00
  • @user1121487 Yes you can create your own implementation, and also implement the `Comparable` interface for that class. Then you can use it in `TreeSet`. – Rohit Jain Oct 09 '13 at 17:00
1

From http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html:

A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.

In other words - you can't do what you're trying to do. While as Rohit Jain pointed out you can do something similar with AstractMap.simpleEntry(..), it seems to me that there are very few use cases in this world where that would be a "good" solution.

Instead, if you're wanting to store key/value pairs in a TreeSet you'll need to create your own Object. That said - why not just use a TreeMap or similar?

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
  • I've been given this interface: public TreeSet> test_1(...); Since it's a TreeSet including Map.Entry value pairs, it must be achievable? – user1121487 Oct 09 '13 at 16:53