Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to print TreeMap values in reverse order?  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below codes find the character frquency how to print this in reverse order using comparator or else
 
Rancher
Posts: 2451
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sumant,

if you look at the API of a TreeMap, you'll see it has a method called 'descendingMap()'.
See if you can use this.

Greetz,
Piet
 
Sumant Mehta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.
this method sorted the values on the basis of character but i want to sort on the basis of integer value .
how can i do this using comparator.
 
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Character and Integer have the same ordering, don't they? Perhaps you could provide an example of what you mean.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the declaration of the MapI think he means he wants to sort by value rather than by key.
 
Piet Souris
Rancher
Posts: 2451
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, one possibility is to create a class say 'CharData', like

and use a TreeMap<Character, CharData>.
Then, create a class that implements Comparator<CharData>, and that compares two instances
according to frequency and, maybe, character.
Then you could use a construction like
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumant Mehta wrote:this method sorted the values on the basis of character but i want to sort on the basis of integer value .
how can i do this using comparator.


Simply put: You can't - at least not in the same Map.

What you could do is get all of its entries, and put their values in a
SortedMap<Integer, Character>
which you can then order any way you like.

HIH

Winston
 
Sumant Mehta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have done in this way:

-------------------------------------------
another class implementing Comparator

-------------------------------------
output:
a:2
b:2
c:2
d:2
f:2
-----------
{a=2}

whats wrong with this code

 
Bartender
Posts: 2155
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumant Mehta wrote:whats wrong with this code


It is not in code tags ;)

How do you know it's wrong?
Please read this and this.
 
Sumant Mehta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not shows the value of other character.
 
Paweł Baczyński
Bartender
Posts: 2155
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you use a TreeMap that orders by comparing number of occurences. All of your characters have the same number of ocurrences.
So your comparator's compareTo methods returns 0 for them. Check in javadoc what TreeMap does when compareTo returns 0 while adding.
 
Sumant Mehta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyOne
finally my problem solved

using this code

--------------------

----------------------------------------------
it sort the map by value in reverse order
 
Bartender
Posts: 3306
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumant Mehta wrote:it sort the map by value in reverse order


Well done and thanks for posting your solution but please do read the link I supplied previously on how to use code tags and please start using them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!