This method returns the highest value of a map. - Java java.util

Java examples for java.util:Map Value

Description

This method returns the highest value of a map.

Demo Code


//package com.java2s;

import java.util.Map;

public class Main {
    /**/*from   w  ww. j av a  2  s.  co  m*/
     * This method returns the highest value of a map.
     * This value class of the map has to implement comparable
     *
     * @param map Map
     * @param <K> Key class
     * @param <V> Value class implementing Comparable
     * @return Highest values in the map
     */
    public static <K, V extends Comparable> V getHighestValue(Map<K, V> map) {
        V highestValue = null;

        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (highestValue == null
                    || entry.getValue().compareTo(highestValue) > 0) {
                highestValue = entry.getValue();
            }
        }

        return highestValue;
    }
}

Related Tutorials