26

我想在 HashSet 和 HashMap 中找到最大的数字。假设我的 HashSet 中有数字 [22,6763,32,42,33],我想在当前的 HashSet 中找到最大的数字..我该怎么做?HashMap 也一样。我希望你能帮助我。谢谢你。

4

8 回答 8

73

您可以使用它Collections.max(Collection)来查找任何集合中的最大元素。同样,对于 a HashMap,您可以在其keySet()or上使用相同的方法values(),具体取决于您想要最大键还是最大值。

此外,如果您愿意,您可以使用TreeSetandTreeMap代替,它将元素按排序键顺序存储。

于 2013-02-12T11:06:41.113 回答
11

尝试

    int max = Collections.max(set);
    int maxKey = Collections.max(map.keySet());
    int maxValue Collections.max(map.values());
于 2013-02-12T11:10:05.340 回答
7

如果您被迫使用HashSet/ HashMap,那么您必须扫描整个HashSet/HashMap才能找到最大值。像这样的库函数Collections.max()会这样做。

如果您想O(1)检索最大值,并且您可以更改正在使用的集合类型,请使用排序集/映射(例如TreeSet/ TreeMap)。

于 2013-02-12T11:07:04.390 回答
2

像这样的东西:

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;
    }
}

还有这个:

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;
    }
}
于 2013-02-12T11:08:16.173 回答
0

在 TreeMap 的情况下,如果您知道键/值是随机插入的,那么树将或多或少是平衡的。树变得不平衡,如果以已经排序的顺序插入数据,那么快速查找(或插入或删除)给定元素的能力将丢失。在不平衡树的情况下,它将花费与 n 成正比的时间,O(n) 否则 O(1)。

于 2013-02-12T11:29:04.670 回答
0

考虑使用Apache Commons Math。这是API 文档
感兴趣的类是SummaryStatistics。它与doubles 一起工作并即时计算最大值、最小值、平均值等(当您向其添加值时)。数据值不存储在内存中,因此此类可用于计算非常大的数据流的统计信息。

于 2014-10-03T06:46:15.670 回答
0

这是一个简单的方法,可以满足您的要求:

  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;
}
于 2016-09-17T21:27:03.080 回答
0

注意:如果您想从 Map 中找到最大值,请尝试使用 maxEntry.get().getValue() 而不是 maxEntry.get().getKey()

1. 使用流

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. 将 Collections.max() 与 Lambda 表达式一起使用

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. 使用 Stream 和方法参考

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. 使用 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. 使用简单迭代

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();
}
于 2019-01-25T15:22:50.483 回答