在封闭范围内定义的局部变量日志必须是最终的或有效的最终 [英] Local variable log defined in an enclosing scope must be final or effectively final

查看:142
本文介绍了在封闭范围内定义的局部变量日志必须是最终的或有效的最终的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是lambda和Java8的新手。我正面临以下错误。

I'm new to lambda and Java8. I'm facing following error.


在封闭范围内定义的局部变量日志必须是final或
有效最终

Local variable log defined in an enclosing scope must be final or effectively final



public JavaRDD<String> modify(JavaRDD<String> filteredRdd) {

    filteredRdd.map(log -> {

        placeHolder.forEach(text -> {

            //error comes here
            log = log.replace(text, ",");

        });

        return log;

    });

    return null;
}


推荐答案

消息说明究竟是什么问题是:你的变量日志必须是final(即:携带关键字final)或者是有效的final(也就是说:你只在lambda之外为一次分配一个值)。

The message says exactly what the problem is: your variable log must be final (that is: carry the keyword final) or be effectively final (that is: you only assign a value to it once outside of the lambda).

否则,您不能在lambda语句中使用该变量。
当然,这与您使用日志有冲突。重点是:你不能在lambda中写一些外在的东西......所以你必须退后一步,寻找其他方法来做你想做的事情。

Otherwise, you can't use that variable within your lambda statement. But of course, that conflicts with your usage of log. The point is: you can't write to something external from within the lambda ... so you have to step back and look for other ways for whatever you intend to do.

从这个意义上讲:只要相信编译器。和侧节点:而不是在这里发布错误消息;您可以尝试先将其谷歌(这称为先前研究,实际上是您希望做的事情)。

In that sense: just believe the compiler. And side node: instead of posting the error message here; you could try to first google it (that is called "prior research" and actually something that is expected from you to do).

这篇关于在封闭范围内定义的局部变量日志必须是最终的或有效的最终的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆