英文:
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("Q04")) {
return QTable.get.maxValue("Q40", "Q43", "Q45") }
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("Q40", "Q43", "Q45")
.map(hashMap::get)
.mapToDouble(value -> 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<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));
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论