Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java – Displaying key in map with most occurrences – Java

I have a string and i created a map which schows occurrences of characters:

String: "aaa bbbb ccccccccccc ddddd eeee"
map: {a=3, b=4, c=11, d=5, e=4}

And i want to display only c, also output:

c

(because it occurres most of the times).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can find the max key using streams like this:

Character maxKey = map.entrySet().stream()
    .max(Map.Entry.comparingByValue())
    .get().getKey();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading