Java 试图更新CompletableFuture变量,但出现错误:从lambda表达式引用的局部变量必须是final或有效final public CompletableFuture description(){ CompletableFuture结果=CompletableFuture .SupplySync(()->“搜索:“+this.stop+”“+name+”:\n”) 。然后应用(x->x+=“From”+this.stop+“\n”); CompletableFuture temp=服务。然后接受(x->{ 用于(总线服务:x.keySet()){ CompletableFuture stops=x.get(服务); 结果=结果。然后应用(y->y+=描述服务(服务,停止)); } }); 返回结果; } 公共完整的未来描述服务(总线服务, 可完成(未来站点){ 返回停止。然后应用(x->{ if(x.isEmpty()){ 返回“”; } 返回x.stream() .filter(停止->停止!=此.stop) .reduce(“-可以将“+服务+”转换为:\n”, (str,stop)->str+=“-”+stop+“\n”, (str1,str2)->str1+str2); }); }
我试图更新Java 试图更新CompletableFuture变量,但出现错误:从lambda表达式引用的局部变量必须是final或有效final public CompletableFuture description(){ CompletableFuture结果=CompletableFuture .SupplySync(()->“搜索:“+this.stop+”“+name+”:\n”) 。然后应用(x->x+=“From”+this.stop+“\n”); CompletableFuture temp=服务。然后接受(x->{ 用于(总线服务:x.keySet()){ CompletableFuture stops=x.get(服务); 结果=结果。然后应用(y->y+=描述服务(服务,停止)); } }); 返回结果; } 公共完整的未来描述服务(总线服务, 可完成(未来站点){ 返回停止。然后应用(x->{ if(x.isEmpty()){ 返回“”; } 返回x.stream() .filter(停止->停止!=此.stop) .reduce(“-可以将“+服务+”转换为:\n”, (str,stop)->str+=“-”+stop+“\n”, (str1,str2)->str1+str2); }); },java,Java,我试图更新description()中forloop中的结果,因为结果。然后apply()生成一个新的可完成的未来实例,我需要将其重新分配给一个新变量以更新结果,但我不太确定如何不需要将其重新分配给新变量,你不应该。而是将未来结合起来 public CompletableFuture<String> description() { CompletableFuture<String> result = CompletableFuture .s
description()
中forloop中的结果,因为结果。然后apply()
生成一个新的可完成的未来实例,我需要将其重新分配给一个新变量以更新结果,但我不太确定如何不需要将其重新分配给新变量,你不应该。而是将未来结合起来
public CompletableFuture<String> description() {
CompletableFuture<String> result = CompletableFuture
.supplyAsync(() -> "Search for: " + this.stop + " <-> " + name + ":\n")
.thenApply(x -> x += "From " + this.stop + "\n");
CompletableFuture<Void> temp = services.thenAccept(x -> {
for (BusService service : x.keySet()) {
CompletableFuture<Set<BusStop>> stops = x.get(service);
result = result.thenApply(y -> y += describeService(service, stops));
}
});
return result;
}
public CompletableFuture<String> describeService(BusService service,
CompletableFuture<Set<BusStop>> stops) {
return stops.thenApply(x -> {
if (x.isEmpty()) {
return "";
}
return x.stream()
.filter(stop -> stop != this.stop)
.reduce("- Can take " + service + " to:\n",
(str, stop) -> str += " - " + stop + "\n",
(str1, str2) -> str1 + str2);
});
}
返回服务。然后合并(结果,(x,y)->{
用于(总线服务:x.keySet()){
CompletableFuture stops=x.get(服务);
y+=描述服务(服务、停止);
}
返回y;
});
它可以编译,而且似乎可以工作,但是,我遇到了这个错误:jdk.internal.net.http.common。MinimalFuture@6f6c1eaa[未完成](id=589)jdk.internal.net.http.common。。。。。。(知道为什么吗?)。
return services.thenCombine(result, (x, y) -> {
for (BusService service : x.keySet()) {
CompletableFuture<Set<BusStop>> stops = x.get(service);
y += describeService(service, stops);
}
return y;
});