我是 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.reversed
和Comparator.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/