gpt4 book ai didi

Java 8流 "Cannot use this in a static context"

转载 作者:行者123 更新时间:2023-12-01 20:54:44 24 4
gpt4 key购买 nike

我是 java8 流的新手,对于这个愚蠢的问题感到抱歉。这是我的代码,我试图创建 id 和值的映射,但我收到此错误,无法修复。谁能帮我选择什么?

    public static Map<Integer, String> findIdMaxValue(){
Map<Integer, Map<String, Integer>> attrIdAttrValueCountMap = new HashMap<>();
Map<Integer, String> attrIdMaxValueMap = new HashMap<>();
attrIdAttrValueCountMap.forEach((attrId, attrValueCountMap) -> {
attrValueCountMap.entrySet().stream().sorted(this::compareAttrValueCountEntry).findFirst().ifPresent(e -> {
attrIdMaxValueMap.put(attrId, e.getKey());
});
});
}

及排序方法

public static int compareAttrValueCountEntry(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
int diff = e1.getValue() - e2.getValue();
if (diff != 0) {
return -diff;
}
return e1.getKey().compareTo(e2.getKey());
}

我收到此错误

"Cannot use this in a static context"

最佳答案

您的代码存在几个问题。而this::compareAttrValueCountEntry会很容易通过将其更改为 ContainingClassName::compareAttrValueCountEntry 来修复,这个方法是不必要的因为有几个工厂方法,例如 Map.Entry.comparingByKey , Map.Entry.comparingByValue , Comparator.reversedComparator.thenComparing ,可以组合起来实现相同的目标

这可以防止您在 compareAttrValueCountEntry 中犯下错误。 。比较 int 很诱人通过减去值,但这很容易出错,因为两个 int 之间的差异值(value)观并不总是适合 int范围,因此可能会发生溢出。另外,否定颠倒顺序的结果是损坏,因为该值可能是 Integer.MIN_VALUE ,没有正对应项,因此否定它将溢出回Integer.MIN_VALUE而不是改变标志。

而不是通过forEach循环要添加到另一个 map ,您可以使用更清晰的流操作来生成 map ,您可以简化sorted(…).findFirst()min(…)这不仅更短,而且可能更便宜的操作。

把它们放在一起,我们得到

Map<Integer, String> attrIdMaxValueMap =
attrIdAttrValueCountMap.entrySet().stream()
.filter(e -> !e.getValue().isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,
e -> e.getValue().entrySet().stream()
.min(Map.Entry.<String, Integer>comparingByValue().reversed()
.thenComparing(Map.Entry.comparingByKey())).get().getKey()));

请注意,我在前面添加了 filter拒绝空映射的操作,这确保了总会有一个匹配的元素,所以不需要处理 ifPresent或类似的。相反,Optional.get可以无条件调用。

因为这个方法被称为findIdMaxValue ,可能希望通过调用 max 来反射(reflect)这一点在流上而不是 min ,这只是反转哪个比较器的问题:

Map<Integer, String> attrIdMaxValueMap =
attrIdAttrValueCountMap.entrySet().stream()
.filter(e -> !e.getValue().isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,
e -> e.getValue().entrySet().stream()
.max(Map.Entry.<String, Integer>comparingByValue()
.thenComparing(Map.Entry.comparingByKey(Comparator.reverseOrder())))
.get().getKey()));

不幸的是,这样的构造遇到了类型推断的局限性,这要求我们:使用嵌套结构(如 Map.Entry.comparingByKey(Comparator.reverseOrder()) 而不是 Map.Entry.comparingByKey().reversed() ) 或插入显式类型,例如 Map.Entry.<String, Integer>comparingByValue() 。在第二个变体中,反转第二个比较器,我们遇到了两次限制……

在这种特定情况下,可能需要只创建一次比较器,将其保存在变量中并在流操作中重用它:

Comparator<Map.Entry<String, Integer>> valueOrMinKey
= Map.Entry.<String, Integer>comparingByValue()
.thenComparing(Map.Entry.comparingByKey(Comparator.reverseOrder()));

Map<Integer, String> attrIdMaxValueMap =
attrIdAttrValueCountMap.entrySet().stream()
.filter(e -> !e.getValue().isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,
e -> e.getValue().entrySet().stream().max(valueOrMinKey).get().getKey()));

关于Java 8流 "Cannot use this in a static context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42545325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com