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();