1

OK, I have

Set<String> s;
HashMap<String, Double> hm;

And, I want to find keys in hm which are related to the maximum values among all possible key(candidate)s in the set s.

Below method is what I already have, which helps me to find multiple keys regarding to one values. I might use Collections.Max(hm.values()) to get the maximum value

public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
    Set<T> keys = new HashSet<T>();
    for (Entry<T, E> entry : map.entrySet()) {
        if (Objects.equals(value, entry.getValue())) {
            keys.add(entry.getKey());
        }
    }
    return keys;
}

What would be the 'neat' solution that you can suggest?

I was working with Python for past few months, and now it is so inconvenient to deal with all sort of Maps instead of dictionaries by Java.


What I want to remark on this question is, simplifying below code in somewhat 'smarter' way in Java.

import com.google.common.collect.Sets;

double maxVal = 0.0;

for(String candidate : s){
    if(hm.get(candidate) >= maxVal){
        maxVal = hm.get(candidate);
    }
}
Set<String> subset = Sets.intersection(set, getKeysByValue(hm, maxVal));

Possible Python-like implementation (List comprehension) is

subset = set.intersection(s, getKeysByValue(hm, Collections.max([hm.get(item) for item in s]))
SUNDONG
  • 2,501
  • 5
  • 21
  • 37
  • Check [Finding Key associated with max Value in a Java Map](http://stackoverflow.com/questions/5911174/finding-key-associated-with-max-value-in-a-java-map) and also read the comments on the accepted answer about multiple max values – sam Oct 28 '15 at 13:01
  • @sam It's a little bit different, only options are elements in set `s` and I am willing to see the detailed implementation skill. If I write it in pythonic style, the answer would be something like `Answer set = getKeysByValue(hm, Collections.max([hm.get(item) for item in s]))` – SUNDONG Oct 28 '15 at 13:05
  • One possible solution would be to Just find max value in set and then use that value to compare to the values in map – sam Oct 28 '15 at 13:12
  • @sam Yes, that is what I was thinking of. Do you have any neat implementation instead of iterating over set by for loop? – SUNDONG Oct 28 '15 at 13:14

3 Answers3

0

So you want to check the value of your hash map and if it corresponds to an entry in your set that key & value pair is then added to your condensed hash map ?

In Python

def get_matching_values(my_set, my_dict):
    new_dict = {}
    for key, value in my_dict.iteritems():
        if value in my_set:
            new_dict[key] = value

    return new_dict

or as a dictionary comprehension

def get_matching_values(my_set, my_dict):
    return dict((key, value) for key, value in my_dict.iteritems() if value in my_set)
Christian Witts
  • 11,375
  • 1
  • 33
  • 46
0

If you used SortedSet (TreeSet) instead of Set you could call SortedSet.last() to get the max value.

lance-java
  • 25,497
  • 4
  • 59
  • 101
0

As I said in the comments, you can first retrieve max value from the set using Collections.max(...) and use that value to compare the values in the map

Set<Integer> set = new HashSet<Integer>(Arrays.asList(10,30,20,10));
int maxValueInSet = Collections.max(set);
    
Map<String, Integer> hashMap = new HashMap<String, Integer>() {{
    put("a", 20);
    put("b", 30);
    put("c", 10);
    put("d", 40);
    put("e", 30);
}};
    
for (Map.Entry<String, Integer> entry: hashMap.entrySet()) {
    if (entry.getValue().equals(maxValueInSet)) {
        System.out.println(entry.getKey());
    }
}

Output:

b

e

Demo

Community
  • 1
  • 1
sam
  • 2,033
  • 2
  • 10
  • 13
  • My intention for set was `Set set = new HashSet(Arrays.asList("a", "b", "e"));` – SUNDONG Oct 28 '15 at 13:42
  • @SUNDONG List is string and set is integer which wont compile. Assuming that set is an integer, do you want to compare the max string value in set to max key in map? – sam Oct 28 '15 at 13:46
  • I put additional paragraph on my original question, you might understand my point then. – SUNDONG Oct 28 '15 at 13:48
  • @SUNDONG Oh I just saw that you already have done what I said in the answer. I am not aware of any other way to do than what you have done. You might want to post it on codereview site where you can get feedback on the working code – sam Oct 28 '15 at 13:56