Java HashMap – Get highest Key or highest value

Java HashMaps are a key : value data object. They are widely used in Java and are based on the Map interface. Their flexibility lies in their ability to span just about any data type you can think of, including custom objects. They offer fast retrieval of the value. Keys must be unique in HashMaps therefore attempting to insert two keys of the same name results in the previous key being overwritten.

When we declare a Java HashMap, we also declare the data types it holds. A simple Java HashMap might be;

HashMap<String, String> myHM = new HashMap<String, String>();

This indicates that the key is of type String and the value is of type String. A more complex HashMap might be in the format;



HashMap<String, myCustomObject> myHM = new HashMap<String, myCustomObject>();

We can also include sub-hashmaps;

HashMap<String, HashMap<String, Integer>> myHM = new HashMap<String, HashMap<String, Integer>>();

For the purposes of this exercise, we want to have a HashMap of type Integer;

HashMap<Integer, Integer> myHM = new HashMap<Integer, Integer>();

If we want to find the highest key, we could write a method like the below;

public Integer getHighestKeyFromHM(HashMap<Integer, Integer> inputHM){

        Integer highestKey=-1;
        for(Map.Entry<Integer, Integer> entries : inputHM.entrySet()){
            if(entries.getKey() > highestKey){
                highestKey = entries.getKey();
            }
        }
        return highestKey;
    }

This will return the highest key. If there are no entries, it returns -1.

In a similar fashion, we could find the highest value of a HashMap;

public Integer getHighestValueFromHM(HashMap<Integer, Integer> inputHM){

        Integer highestValue=-1;
        for(Map.Entry<Integer, Integer> entries : inputHM.entrySet()){
            if(entries.getValue() > highestValue){
                highestValue = entries.getValue();
            }
        }
        return highestValue;
    }

This returns the actual value, we could also have it return the key holding the highest value;

public Integer getHighestValueFromHM(HashMap<Integer, Integer> inputHM){

        Integer highestValue=-1;
        for(Map.Entry<Integer, Integer> entries : inputHM.entrySet()){
            if(entries.getValue() > highestValue){
                highestValue = entries.getKey();
            }
        }
        return highestValue;
    }

A shorter way using Java Collections of obtaining the highest value is (‘myHm’ refers to the name of your HashMap);

Integer  maxValueInMap = (Collections.max(myHM.values()));

If it is likely that many values will have the same value, then you could fall foul of missing entries that have the same highest value. In this case, having the above function return the highest value and then the below function to return a List of entries is possible;

public ArrayList<Integer> getHighestValuesListFromHM(HashMap<Integer, Integer> inputHM, Integer highestValue){

ArrayList<Integer> returnList = new ArrayList<Integer>();
        
        for(Map.Entry<Integer, Integer> entries : inputHM.entrySet()){
            if(entries.getValue() >= highestValue){
returnList.add(entries.getKey());
                

            }
        }
        return returnList;
    }

Related Posts

Records in Java

Records were a new addition in Java 14. Before that, data was typically held in a class with Getter and Setter methods. Typically, these storage classes were used to store…

Getting a record from HBase

Getting a record from HBase is similar to getting a record from SQL. You still need basic details like database name, table name and a row key. Where it differs…

You Missed

Global IT Outage : All eyes on CrowdStrike

  • By aCoder
  • July 19, 2024
  • 1302 views
Global IT Outage : All eyes on CrowdStrike

Java HashMap – Get highest Key or highest value

  • By aCoder
  • July 17, 2024
  • 1692 views
Java HashMap – Get highest Key or highest value

The problem with frameworks …

  • By aCoder
  • July 12, 2024
  • 1401 views
The problem with frameworks …

A.I. stocks are in bubble territory

  • By aCoder
  • July 10, 2024
  • 1253 views
A.I. stocks are in bubble territory

The AI Boyfriend

  • By aCoder
  • July 9, 2024
  • 1309 views
The AI Boyfriend

WordPress – only index page showing

  • By aCoder
  • July 2, 2024
  • 1245 views
WordPress – only index page showing