lambda表达式中使用的变量应该是final或有效final

当我尝试使用calTz时,它显示了这个错误。

private TimeZone extractCalendarTimeZoneComponent(Calendar cal, TimeZone calTz) {
    try {
        cal.getComponents().getComponents("VTIMEZONE").forEach(component -> {
            VTimeZone v = (VTimeZone) component;
            v.getTimeZoneId();
            if (calTz == null) {
                calTz = TimeZone.getTimeZone(v.getTimeZoneId().getValue());
            }
        });
    } catch (Exception e) {
        log.warn("Unable to determine ical timezone", e);
    }
    return null;
}

当前回答

你不能在lambda表达式中使用来自lambda作用域之外的新引用重新分配变量。但是您当然可以修改对象的现有状态。因此,取而代之的是将'calTz'重新分配给新引用。你可以调用它的setter方法来改变它的内部状态。所以这将工作(如果你的VtimeZone是可变的):

    calTz=new TimeZone();    
cal.getComponents().getComponents("VTIMEZONE").forEach(component -> {
                         VTimeZone v = (VTimeZone) component;
                    v.getTimeZoneId();
                    
                        calTz.setTimeZoneId("some value");
                    
                })

但是这不是一个好的习惯。 以上代码也可以由。

        if(calTz == null){
    calTz=new TimeZone();
cal.getComponents().getComponents("VTIMEZONE").get(0).setTimeZoneId("some value");}

其他回答

尽管其他答案证明了这个需求,但它们并没有解释这个需求存在的原因。

JLS在§15.27.2中提到了原因:

对有效最终变量的限制禁止访问动态更改的局部变量,捕获这些局部变量可能会引入并发性问题。

为了降低错误的风险,他们决定确保捕获的变量永远不会发生突变。


这也适用于匿名内部类

lambda表达式中使用的变量应该是final或有效的final JAVA

要以不优雅的方式解决这个问题,有两个问题: 副作用和穿线问题

final AtomicInteger e = new AtomicInteger(0);
        new Thread(() -> {
            e.addAndGet(1);
        });

更准确地说,我同意是一种相同的,但使用Lambda函数背后的想法是避免副作用,当我们访问Lambda函数中的最终引用来填充值以从外部获得结果时,我们打破了这个概念。

在最老的帖子中,你可能想这样重写

cal.getComponents () .getComponents (VTIMEZONE) .streams () . map (v - > v.getTimeZoneId () .getValue ()) .collect (Collectors.toList ());

对于线程方面,我们也有同样的副作用问题,此外,你永远不知道什么时候访问原子变量来收集结果,你可以放一个CountDownLatch…最好使用CompletableFuture来处理结果和同步方面

Java 8有一个叫做“有效最终”变量的新概念。这意味着一个在初始化后值从未改变的非最终局部变量被称为“有效最终”。

引入这个概念是因为在Java 8之前,我们不能在匿名类中使用非最终局部变量。如果你想在匿名类中访问一个局部变量,你必须将它设为final。

引入lambda后,这种限制得到了缓解。因此,如果局部变量在初始化为lambda后没有改变,那么它就需要成为final,因为lambda本身只是一个匿名类。

Java 8意识到了每次开发人员使用lambda时声明局部变量为final的痛苦,引入了这个概念,并使局部变量不再需要为final。因此,如果您看到匿名类的规则没有改变,那么在使用lambdas时,您不必每次都写最后一个关键字。

我在这里找到了一个很好的解释

final变量意味着它只能被实例化一次。 在Java中,你不能在lambda和匿名内部类中重新分配非最终局部变量。

你可以用旧的for-each循环重构你的代码:

private TimeZone extractCalendarTimeZoneComponent(Calendar cal,TimeZone calTz) {
    try {
        for(Component component : cal.getComponents().getComponents("VTIMEZONE")) {
        VTimeZone v = (VTimeZone) component;
           v.getTimeZoneId();
           if(calTz==null) {
               calTz = TimeZone.getTimeZone(v.getTimeZoneId().getValue());
           }
        }
    } catch (Exception e) {
        log.warn("Unable to determine ical timezone", e);
    }
    return null;
}

即使我不明白这段代码的某些部分:

你调用v.getTimeZoneId();不使用它的返回值 赋值calTz = TimeZone.getTimeZone(v.getTimeZoneId().getValue());你不会修改最初传递的calTz,也不会在这个方法中使用它 你总是返回null,为什么不把void设为返回类型呢?

希望这些建议也能帮助你提高。

lambda表达式中使用的变量应该是final或有效的final,但可以将值赋给最终的单元素数组。

private TimeZone extractCalendarTimeZoneComponent(Calendar cal, TimeZone calTz) {
    try {
        TimeZone calTzLocal[] = new TimeZone[1];
        calTzLocal[0] = calTz;
        cal.getComponents().get("VTIMEZONE").forEach(component -> {
            TimeZone v = component;
            v.getTimeZoneId();
            if (calTzLocal[0] == null) {
                calTzLocal[0] = TimeZone.getTimeZone(v.getTimeZoneId().getValue());
            }
        });
    } catch (Exception e) {
        log.warn("Unable to determine ical timezone", e);
    }
    return null;
}