gpt4 book ai didi

java - 错误 : incompatible types: inference variable R has incompatible bounds (Lambda java 8)

转载 作者:行者123 更新时间:2023-11-30 06:50:30 25 4
gpt4 key购买 nike

我有一个 Tree 对象,其中包含 Tree 对象的子对象 (HashMap) 等等。
我需要通过 numericPosition 变量过滤对象。

例如:

Tree mapTreeRoot = new Tree("Root",0);      
int answer = 111;

mapTreeRoot
.addNode("ChilldOfRoot",111)
.addNode("ChildOfRootExample1",222)
.addNode("ChildOfRootExample1Example2",333);

Tree treeObj = mapTreeRoot
.children
.entrySet().stream()
.filter(map -> answer == map.getValue().numericPosition)
.collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

在这种情况下,我应该得到一个由 numericPosition 过滤的 Tree 对象
树类

   public Tree(String name,int numericPosition) {
this.name = name;
this.numericPosition = numericPosition;
}

public Tree addNode(String key,int numericPosition) {
Object hasKey = children.get(key);
if(hasKey == null) {
children.put(key,new Tree(key,numericPosition));
}

return children.get(key);
}

public Tree getNode(String key) {
Object hasKey = children.get(key);
if(hasKey != null) {
return children.get(key);
}
return this;
}

以防万一
我收到此错误:错误:不兼容的类型:推理变量 R 具有不兼容的边界

我一直在关注这个例子,但它对我不起作用。 https://www.mkyong.com/java8/java-8-filter-a-map-examples/

我也试过HashMap<String,Tree> treeObj = mapTreeRoot .. 但收到相同的错误消息。

最佳答案

如果您只想过滤一棵树,您可以使用:

Tree treeObj = null;
Optional<Entry<String, Tree>> optional = mapTreeRoot
.children
.entrySet().stream()
.filter(map -> answer == map.getValue().numericPosition)
.findAny();
if(optional.isPresent()){
treeObj = optional.get().getValue();
}

关于java - 错误 : incompatible types: inference variable R has incompatible bounds (Lambda java 8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41335606/

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