error: incompatible types: inference variable R has incompatible bounds (Lambda java 8)
error: incompatible types: inference variable R has incompatible bounds (Lambda java 8)
我有一个 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 对象
树class
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();
}
我有一个 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 对象
树class
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();
}