简体   繁体   中英

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

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.

Something like this:

Double max = Stream.of("Q40", "Q43", "Q45")
             .map(hashMap::get)
             .mapToDouble(value -> value)
             .max()
             .getAsDouble();
        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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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