Find the Biggest number in HashSet/HashMap java

63,773

Solution 1

You can use Collections.max(Collection) to find the maximum element out of any collection. Similarly, for a HashMap, you can use the same method on its keySet() or values(), depending upon whether you want maximum key, or maximum value.

Also, if you want as such, you can use a TreeSet and TreeMap instead, that stores the elements in sorted key order.

Solution 2

try

    int max = Collections.max(set);
    int maxKey = Collections.max(map.keySet());
    int maxValue Collections.max(map.values());

Solution 3

If you are forced to use a HashSet/HashMap, then you have to scan the whole HashSet/HashMap in order to find the maximum. Library functions like Collections.max() will do like this.

If you want O(1) retrieval of the maximum, and you are allowed to change the type of collection being used, use a sorted set/map (e.g. TreeSet/TreeMap).

Solution 4

Something like this:

Set<Integer> values = new HashSet<Integer>() {{
    add(22);
    add(6763);
    add(32);
    add(42);
    add(33);
}};
int maxValue = Integer.MIN_VALUE;
for (int value : values) {
    if (value > maxValue) {
        maxValue = value;
    }
}

And this:

Map<String, Integer> values = new HashMap<String, Integer>() {{
    put("0", 22);
    put("1", 6763);
    put("2", 32);
    put("3", 42);
    put("4", 33);
}};
int maxValue = Integer.MIN_VALUE;
for (int value : values.values()) {
    if (value > maxValue) {
        maxValue = value;
    }
}
Share:
63,773

Related videos on Youtube

user2064467
Author by

user2064467

Updated on January 26, 2020

Comments

  • user2064467
    user2064467 over 4 years

    I would like to find the biggest number in HashSet and HashMap. Say I have the number [22,6763,32,42,33] in my HashSet and I want to find the largest number in my current HashSet..how would i do this? and Same thing for the HashMap as well. I hope you can help me with it. Thank you.

    • David Schwartz
      David Schwartz over 11 years
      If you need to do this, you probably shouldn't be using a hash-based collection.
  • Marko Topolnik
    Marko Topolnik over 11 years
    O(1) works only on map keys; for values a separate collection must be constructed first, so it's O(n) again, with a very bad constant factor.
  • user2064467
    user2064467 over 11 years
    Wow..Thank you..it helps alot.:)
  • Shiva Kumar
    Shiva Kumar over 11 years
    Also note that TreeSet gets costlier for insertion and deletion. So wisely choose the right implementation
  • Kamil
    Kamil over 10 years
    Terrible way to initialize a HashMap just for that simple purpose a new class has to be created. I hate it every time I see it in code I work with.
  • duffymo
    duffymo over 10 years
    What's so terrible? I see no harm in it.
  • Kamil
    Kamil over 10 years
    What I wrote above, it creates new class just for the purpose of creating map with few elements. It's very slow (there are tests on it on stackoverflow).
  • duffymo
    duffymo over 10 years
    I'll never believe that this would be the bottleneck in any app. Micro-optimization at its worst.
  • gladed
    gladed about 7 years
    Don't forget that max() throws NoSuchElementException on empty collections.