[Java] How to get the maximum value of HashMap

Development environment / version

-Host OS: Windows10 Home ・ Guest OS: WSL2 Ubuntu 20.04 ・ Java8


To get the maximum value of HashMap whose key type is String and value type is Integer.

Implement using the EntrySet method. Below is the actual code

public static void main(String[] args) {
  String arr[] = {"a", "a", "a", "a", "b", "b", "b", "c", "c", "d"};
  Map<String, Integer> map = new HashMap<>();

//Store array elements in a map
  for (String string : arr) {
    if (map.get(string) == null) {
      map.put(string, 1);
    } else {
      map.put(string, map.get(string) + 1);

//Initialize the maximum value and the key at that time
  String maxKey = null;
  Integer maxValue = 0;

//Get the map key and Value one by one with the entrySet method
for (Map.Entry<String, Integer> entry : map.entrySet()) {

//Compare the maximum value and Value, and if Value is large, substitute the key and Value at that time.
  if (entry.getValue() > maxValue) {
    maxKey = entry.getKey();
    maxValue = entry.getValue();
System.out.println(maxKey + " : " + maxValue);


a : 4

I was able to get the maximum value safely. If there are multiple maximum values and you want to get all of them, you can store them in a list.

The advantage of entrySet is that you can get both the key and the value in one loop.

Recommended Posts

[Java] How to get the maximum value of HashMap
[Java] How to get the URL of the transition source
[Java] How to get the current directory
How to get the date in java
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
How to get today's day of the week
[Java] How to get the redirected final URL
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
How to create your own annotation in Java and get the value
How to write Scala from the perspective of Java
As of April 2018 How to get Java 8 on Mac
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
[Swift] How to get the document ID of Firebase
[Java] How to easily get the longest character string of ArrayList using stream
How to use Java HttpClient (Get)
How to get the class name / method name running in Java
An unsupported Java version How to get rid of errors
Get to the abbreviations from 5 examples of iterating Java lists
How to get the longest information from Twitter as of 12/12/2016
How to change the setting value of Springboot Hikari CP
How to derive the last day of the month in Java
[jsoup] How to get the full amount of a document
[Java] How to get the key and value stored in Map by iterative processing
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Get the result of POST in Java
[Rails] How to get the URL of the transition source and redirect
[Java] How to use the File class
[Swift5] How to get an array and the complement of arrays
[Java] Get the day of the specific day of the week
[java] Summary of how to handle char
How to display 0 on the left side of the standard input value
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
How to get the contents of Map using for statement Memorandum
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to sort the List of SelectItem
How to pass the value to another screen
Output of the book "Introduction to Java"
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java] How to convert from String to Path type and get the path
How to check for the contents of a java fixed-length string
How to output the sum of any three numbers, excluding the same value
How to change the maximum and maximum number of POST data in Spark
[Java] [ibatis] How to get records of 1-to-N relationship with List <Map <>>
How to find out the Java version of a compiled class file
How to find the total number of pages when paging in Java
How to get the setting value (property value) from the database in Spring Framework
How to change the value of a variable at a breakpoint in intelliJ
[Java improvement case] How to reach the limit of self-study and beyond
I want to get the field name of the [Java] field. (Old tale tone)
Android development, how to check null in the value of JSON object
How to find the cause of the Ruby error