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;
}







