どうも、ちょげ(@chogetarou)です。
Map(マップ)のキー(key)の最大値を取得する方法を紹介します。
方法

Map(マップ)のキー(key)の最大値を取得するには、Collections.max()を使います。
まず、Collections.max()を呼び出します。
そして、Collections.max()の引数で、MapからkeySet()を呼び出します。
Integer maxValue = Collections.max(map.keySet());
上記のCollections.max()
は、keySet()を呼び出したMap(マップ)のキーの最大値を取得します。
使用例
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Map<Integer, String> numbers = new HashMap<Integer, String>();
numbers.put(1, "one");
numbers.put(2, "two");
numbers.put(10, "ten");
numbers.put(3, "three");
numbers.put(4, "four");
numbers.put(5, "five");
Integer maxValue = Collections.max(numbers.keySet());
System.out.println(maxValue);
}
}
出力:
10
コメント