简体   繁体   English

在Hashmap的Hashmap中查找最大值

[英]Finding maximum value in a Hashmap of Hashmaps

So I'm still relatively new to Java and this question may be fairly trivial but I'm having issues getting an answer. 所以我对Java仍然相对较新,这个问题可能相当微不足道,但我在回答问题时遇到了问题。

I'm trying to find the max value within a hashmap I created. 我正在尝试在我创建的hashmap中找到最大值。 I initialized the hashmap and filled it but here's the structure: 我初始化了hashmap并填充了它,但这里是结构:

HashMap<Double,HashMap<Double,Double>>

any help would be awesome. 任何帮助都是极好的。

Thanks! 谢谢!

EDIT: For clarification I would like to know the value of largest entry in the hashtable 编辑:为了澄清我想知道哈希表中最大条目的值

So you have a root map full of nested maps. 所以你有一个充满嵌套地图的根映射。 You will iterate on all values in the root map - fetching nested maps. 您将迭代根映射中的所有值 - 获取嵌套映射。 And then iterate on each value on each nested map. 然后迭代每个嵌套映射上的每个值。

Compexity will be O(n) where n - number of all keys in the all nested maps plus number of keys in root map. Compexity将为O(n),其中n - 所有嵌套映射中所有键的数量加上根映射中的键数。

I think you can not find the max value faster if you use the HashMaps. 如果您使用HashMaps,我认为您无法更快地找到最大值。

The code will look like: 代码如下所示:

final HashMap<Double, HashMap<Double, Double>> data = new HashMap<Double, HashMap<Double, Double>>();

Double prev = null;
for (final HashMap<Double, Double> nestedMap : data.values()) {
    for (final Double value : nestedMap.values()) {
        if (prev == null) {
            prev = value;
            continue;
        }
        prev = Math.max(value, prev);
    }
}

System.out.println("The max value is " + prev);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM