简体   繁体   English

获取在封闭 scope 中定义的局部变量必须是最终的或有效的最终

[英]Getting local variable defined in the enclosing scope must be final or effective final

I am getting "local variable defined in the enclosing scope must be final or effective final" as highlighted below.我得到“在封闭的 scope 中定义的局部变量必须是最终的或有效的最终变量”,如下所示。 I am not sure how to fix this out in lambda expression.我不确定如何在 lambda 表达式中解决这个问题。

在此处输入图像描述

synchronizationVBOVO.getProductVO().getProducts().forEach(eachProduct -> {
        Products product = v1UserIdaasEntitlementRequest.new Products();
        product.setName(eachProduct.getProductName());
        Product entitleProduct = entitlementResponse.getProducts().stream()
                .filter(entitleproduct -> entitleproduct.getName().equalsIgnoreCase(eachProduct.getProductName()))
                .findAny().orElse(null);
        List<Packages> packages = new ArrayList<>();
        Packages pack = v1UserIdaasEntitlementRequest.new Packages();
        eachProduct.getPackages().forEach(productPack -> {
            pack.setName(productPack.getPack().getPackageName());
            Package entitlePackage = new Package();
            if(null != entitleProduct && null != entitleProduct.getPackages() && !entitleProduct.getPackages().isEmpty()) {
                 entitlePackage =  entitleProduct.getPackages().stream().filter(
                    entitlePack -> 
                        entitlePack.getName().equalsIgnoreCase(productPack.getPack().getPackageName()))
                    .findAny().orElse(null);
            }
            List<RumLimits> rumLimits = new ArrayList<>();
            RumLimits rumlimit = v1UserIdaasEntitlementRequest.new RumLimits();
            productPack.getPack().getRumLimits().forEach(rumLimit -> {
                setRumLimits(entitlePackage, rumLimits, rumlimit, rumLimit);
                Wallet wallet = v1UserIdaasEntitlementRequest.new Wallet();
                setWallet(v1UserIdaasEntitlementRequest, pack, entitlePackage, wallet);
            });
            pack.setRumLimits(rumLimits);
            packages.add(pack);
        });
        product.setPackages(packages);
        allProducts.add(product);
    });

You are changing the value of variable entitlePackage .您正在更改变量entitlePackage的值。 You are assigning a value, namely new Package() but in the if statement you are assigning a different value.您正在分配一个值,即new Package()但在if语句中您正在分配一个不同的值。

effectively final means the variable must be assigned a value once only.实际上 final意味着变量必须只被赋值一次。

You can assign a value once only by adding an else to the if .您只能通过将else添加到if来分配一次值。

Package entitlePackage;
if(null != entitleProduct && null != entitleProduct.getPackages() && !entitleProduct.getPackages().isEmpty()) {
    entitlePackage =  entitleProduct.getPackages().stream().filter(
                    entitlePack -> 
                        entitlePack.getName().equalsIgnoreCase(productPack.getPack().getPackageName()))
                    .findAny().orElse(null);
}
else {
    entitlePackage = new Package();
}

Try replacing this:尝试替换这个:

Package entitlePackage = new Package();
if(null != entitleProduct && null != entitleProduct.getPackages() && !entitleProduct.getPackages().isEmpty()) {
                 entitlePackage =  entitleProduct.getPackages().stream().filter(
                    entitlePack -> 
                        entitlePack.getName().equalsIgnoreCase(productPack.getPack().getPackageName()))
                    .findAny().orElse(null);
}

by this:这样:

Package entitlePackage =
        Optional.ofNullable(entitleProduct)
                .map(Product::getPackages)
                .map(Collection::stream)
                .orElse(Stream.empty())
                .filter(e -> productPack.getPack()
                                        .getPackageName()
                                        .equalsIgnoreCase(e.getName()))
                .findAny()
                .orElseGet(Package::new);

The entitlePackage will be effectively final. entitlePackage实际上是最终的。 Also your code will be much easier to read and maintain.此外,您的代码将更容易阅读和维护。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在封闭作用域中定义的局部变量必须是最终的或有效的最终变量 - Local variable defined in an enclosing scope must be final or effectively final 在封闭 scope 中定义的局部变量迭代必须是最终的或有效的最终 - local variable iteration defined in an enclosing scope must be final or effectively final 在封闭 scope 中定义的局部变量 ObjList 必须是最终的或有效的最终 - Local variable ObjList defined in an enclosing scope must be final or effectively final 在封闭范围内定义的局部变量 log 必须是 final 或有效 final - Local variable log defined in an enclosing scope must be final or effectively final 线程:封闭范围中定义的局部变量必须是final或有效的final - Threads: Local variable defined in an enclosing scope must be final or effectively final 我在封闭作用域中定义的局部变量必须是最终的或有效的最终变量 - Local variable i defined in an enclosing scope must be final or effectively final 如果我不断收到错误怎么办:在封闭作用域中定义的局部变量 j 必须是最终的或有效的最终 - What to do if i keep getting error: Local variable j defined in an enclosing scope must be final or effectively final 封闭范围中定义的局部变量计数必须是最终错误 - Local variable count defined in an enclosing scope must be final error 处理封闭 scope 中定义的局部变量的流问题必须是最终的 - working with streams problem of Local variable defined in an enclosing scope must be final Java 8 显示此错误。 在封闭作用域中定义的局部变量 itemList 必须是 final 或有效 final - Java 8 shows this error. Local variable itemList defined in an enclosing scope must be final or effectively final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM