Find the Biggest number in HashSet/HashMap java

后端 未结 8 2114
我在风中等你
我在风中等你 2020-12-29 04:48

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 Ha

相关标签:
8条回答
  • 2020-12-29 05:04

    Note : If you want to find the biggest value from Map try maxEntry.get().getValue() instead of maxEntry.get().getKey()

    1. Using Stream

    public <K, V extends Comparable<V>> V maxUsingStreamAndLambda(Map<K, V> map) {
        Optional<Entry<K, V>> maxEntry = map.entrySet()
            .stream()
            .max((Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue()
                .compareTo(e2.getValue())
            );
    
        return maxEntry.get().getKey();
    }
    

    2. Using Collections.max() with a Lambda Expression

    public <K, V extends Comparable<V>> V maxUsingCollectionsMaxAndLambda(Map<K, V> map) {
        Entry<K, V> maxEntry = Collections.max(map.entrySet(), (Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue()
            .compareTo(e2.getValue()));
        return maxEntry.getKey();
    }
    

    3. Using Stream with Method Reference

    public <K, V extends Comparable<V>> V maxUsingStreamAndMethodReference(Map<K, V> map) {
        Optional<Entry<K, V>> maxEntry = map.entrySet()
            .stream()
            .max(Comparator.comparing(Map.Entry::getValue));
        return maxEntry.get()
            .getKey();
    }
    

    4. Using Collections.max()

    public <K, V extends Comparable<V>> V maxUsingCollectionsMax(Map<K, V> map) {
        Entry<K, V> maxEntry = Collections.max(map.entrySet(), new Comparator<Entry<K, V>>() {
            public int compare(Entry<K, V> e1, Entry<K, V> e2) {
                return e1.getValue()
                    .compareTo(e2.getValue());
            }
        });
        return maxEntry.getKey();
    }
    

    5. Using Simple Iteration

    public <K, V extends Comparable<V>> V maxUsingIteration(Map<K, V> map) {
        Map.Entry<K, V> maxEntry = null;
        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (maxEntry == null || entry.getValue()
                .compareTo(maxEntry.getValue()) > 0) {
                maxEntry = entry;
            }
        }
        return maxEntry.getKey();
    }
    
    0 讨论(0)
  • 2020-12-29 05:09

    try

        int max = Collections.max(set);
        int maxKey = Collections.max(map.keySet());
        int maxValue Collections.max(map.values());
    
    0 讨论(0)
  • 2020-12-29 05:16

    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.

    0 讨论(0)
  • 2020-12-29 05:18

    Here is a simple method which does what you are asking:

      public String getMapKeyWithHighestValue(HashMap<String, Integer> map) {
        String keyWithHighestVal = "";
    
        // getting the maximum value in the Hashmap
        int maxValueInMap = (Collections.max(map.values()));
    
        //iterate through the map to get the key that corresponds to the maximum value in the Hashmap
        for (Map.Entry<String, Integer> entry : map.entrySet()) {  // Iterate through hashmap
            if (entry.getValue() == maxValueInMap) {
    
                keyWithHighestVal = entry.getKey();     // this is the key which has the max value
            }
    
        }
        return keyWithHighestVal;
    }
    
    0 讨论(0)
  • 2020-12-29 05:20

    In case of TreeMap, if you know the key/values are inserted randomly, the tree will be more or less balanced. Trees become unbalanced, if data is inserted in already sorted order, the capability to quickly find (or insert or delete) a given element is lost. In case of unbalanced tree, it will take time proportional to n, O(n) else O(1).

    0 讨论(0)
  • 2020-12-29 05:25

    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).

    0 讨论(0)
提交回复
热议问题