Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Print the keys from HashMap for specific values  RSS feed

 
dhrubo bhattacharjee
Greenhorn
Posts: 20
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone I am trying to learn Java .I was trying to do some small programs and I found this small excercise on a site called HackerRank.Actually they had a contest before and this was one of the questions:

Given a string s only consisting of lowercase letters ('a'-'z'), you have to print the character that occurs the most number of times in the string. In case of a tie, print the character that comes first in the alphabetical order.


This is my code where I was able to get the number of occurences of each charecter and also the max value but however I cannot understand how do I get the keys which have the max values.Once I am able to get the keys with max values I can complete the next part of the problem which says :
In case of a tie, print the character that comes first in the alphabetical order.





Please advice about how to print the keys where the value = max.

 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never knew there was a max method in the Collections class. That won't work for a Map because the Map interface doesn't extend Collection. You can get the entry set from a Map and iterate it and find the largest value, but you cannot guarantee that it will be in alphabetical order. There is a way to get the entry set into alphabetical order.

There is also a simpler way to do that requiring one linear search through an array.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't think of a simple way to create a Stream to find that maximum.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Can't think of a simple way to create a Stream to find that maximum.

Is this simple?
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that looks simple enough. Well done
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Is this simple?

Don't know about simple, but it's yummy. Have a cow.

Quick question: Do you have to supply all those types, or can you simply write something like:

Winston
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this......but bad things happened:

D:\temp>javac Temp.java
Temp.java:13: error: no suitable method found for thenComparing(Comparator<Entry<K#1,Object>>)
.thenComparing(Map.Entry.comparingByKey().reversed()))
^
method Comparator.thenComparing(Comparator<? super Entry<Object,V>>) is not applicable
(argument mismatch; Comparator<Entry<K#2,Object>> cannot be converted to Comparator<? super Entry<Object,V>>)
method Comparator.<U#1>thenComparing(Function<? super Entry<Object,V>,? extends U#1>,Comparator<? super U#1>) is not applicable
(cannot infer type-variable(s) U#1
(actual and formal argument lists differ in length))
method Comparator.<U#2>thenComparing(Function<? super Entry<Object,V>,? extends U#2>) is not applicable
(cannot infer type-variable(s) U#2
(argument mismatch; Comparator<Entry<K#3,Object>> cannot be converted to Function<? super Entry<Object,V>,? extends U#2>))
where K#1,V,K#2,U#1,T,U#2,K#3 are type-variables:
K#1 extends Comparable<? super K#1>
V extends Comparable<? super V>
K#2 extends Comparable<? super K#2>
U#1 extends Object declared in method <U#1>thenComparing(Function<? super T,? extends U#1>,Comparator<? super U#1>)
T extends Object declared in interface Comparator
U#2 extends Comparable<? super U#2> declared in method <U#2>thenComparing(Function<? super T,? extends U#2>)
K#3 extends Comparable<? super K#3>
1 error


I guess type inference is not able to infer everything...

This works, however:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!