• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Print the keys from HashMap for specific values

 
Greenhorn
Posts: 27
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't think of a simple way to create a Stream to find that maximum.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that looks simple enough. Well done
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic