如何在我的HashMap中找到特定条目的最高值,而不是整个HashMap?

huangapple go评论66阅读模式
英文:

How can I find the highest value of only certain entires in my HashMap rather than the entire HashMap?

问题

我有一个类型为(String,Double)的HashMap(称为QTable),我想创建一个方法,根据特定的String键,返回HashMap中2-4个其他条目的最大Double值。

条目看起来类似于<"Q04",0.0> 例如。

在给定键"Q04"的情况下,我希望该方法返回具有键"Q40"、"Q43"和"Q45"的条目的最大Double值。我认为代码可能是这样的:

if (x.equals("Q04")) {
    return Math.max(Math.max(QTable.get("Q40"), QTable.get("Q43")), QTable.get("Q45"));
}

非必要的背景信息:我正在尝试编写贝尔曼方程以填充Q表,这一步将用于在到达新状态时查找潜在移动的最大Q值。

英文:

I have a HashMap (called QTable) of type (String, Double) and I'd like to create a method which given a certain String key will return the maximum Double value of 2-4 other entries in the HashMap.

The entries look something like <"Q04", 0.0> for example.

In the case that given the key "Q04" I would like the method to return the maximum Double value of entries with keys "Q40", "Q43" & "Q45" I assume it will be something along the lines of:

if (if x == QTable.get(&quot;Q04&quot;)) {
		return QTable.get.maxValue(&quot;Q40&quot;, &quot;Q43&quot;, &quot;Q45&quot;) }

Non-essential background info: I'm trying to code the Bellman equation to fill out a Q table and this step will be used to find the maximum Q value of the potential moves on arrival at a new state.

答案1

得分: 1

像这样的:

Double max = Stream.of("Q40", "Q43", "Q45")
                 .map(hashMap::get)
                 .mapToDouble(value -> value)
                 .max()
                 .getAsDouble();
英文:

Something like this:

Double max = Stream.of(&quot;Q40&quot;, &quot;Q43&quot;, &quot;Q45&quot;)
             .map(hashMap::get)
             .mapToDouble(value -&gt; value)
             .max()
             .getAsDouble();

答案2

得分: 1

final Map<String, Double> hashMap = new HashMap<>();
hashMap.put("Q40", 1.0);
hashMap.put("Q41", 3.0);
hashMap.put("Q49", 2.0);
hashMap.put("Q50", 20.0);

Stream.of("Q40", "Q41", "Q49")
        .map(hashMap::get)
        .max(Comparator.comparing(Double::valueOf))
        .ifPresent(d -> System.out.println("The highest value is: " + d));

稍微更紧凑的更新版本。在这里,我创建了一个键的流,从中我想提取最高的值。我从 hashMap 变量中检索值,作为一个 Double,然后使用 max() 操作符,返回一个 Optional 结果。

英文:
        final Map&lt;String, Double&gt; hashMap = new HashMap&lt;&gt;();
        hashMap.put(&quot;Q40&quot;, 1.0);
        hashMap.put(&quot;Q41&quot;, 3.0);
        hashMap.put(&quot;Q49&quot;, 2.0);
        hashMap.put(&quot;Q50&quot;, 20.0);

        Stream.of(&quot;Q40&quot;, &quot;Q41&quot;, &quot;Q49&quot;)
                .map(hashMap::get)
                .max(Comparator.comparing(Double::valueOf))
                .ifPresent(d -&gt; System.out.println(&quot;The highest value is: &quot; + d));

Updated with a slight more condensed version. Here I create a Stream of the keys, from which I want to extract the highest value of. I retrieve the value, as a Double, from the hashMap variable then use the max() operator, returning an Optional result.

huangapple
  • 本文由 发表于 2020年3月16日 01:01:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/60695430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定