Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

sort TreeMap by value

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • 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: 25798
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 25798
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic