• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sort TreeMap by value

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to sort TreeMap by value.
The following is my test code snippet:



The last line causes the following compile errors.
Please help to fix them.
Thanks.

Bound mismatch: The generic method sortedByValues(Map<K,V>) of type MyPanel is not applicable for the arguments (TreeMap<String,MyString>). The inferred type MyString is not a valid substitute for the bounded parameter <V extends Comparable<? super V>> proj/client/swing MyPanel.java line 315 1310051555427 442022
Type mismatch: cannot convert from SortedSet<Map.Entry<String,MyString>> to Map<String,String> proj/client/swing MyPanel.java line 315 1310051555427 442023
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't show us your MyString class, but the error message is saying that it doesn't implement Comparable<? super MyString>. So presumably it doesn't, and you can't use it in that position.
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You didn't show us your MyString class, but the error message is saying that it doesn't implement Comparable<? super MyString>. So presumably it doesn't, and you can't use it in that position.


I do not have the source code of MyString class.
MyString class has functions such as
boolean equals(Object obj);
boolean equals(MyString arg0);
String toString();
How to fix the compile errors without modifying MyString class?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the error message says

The inferred type MyString is not a valid substitute for the bounded parameter <V extends Comparable<? super V>>


And you have confirmed that it isn't, and that it can't be made to be a valid substitute. So if you're just interested in getting rid of compiler errors, don't use that bounded parameter. I expect that is going to lead to other compiler errors, my guess at one of them being that MyString doesn't have a compareTo method.
reply
    Bookmark Topic Watch Topic
  • New Topic