local variables referenced from a lambda expression must be final or effectively final…
- User
local variables referenced from a lambda expression must be final or effectively final int freq1 = (int) Arrays.stream(rank).filter(a -> a == max1).count();
- Assistant
The code snippet you provided contains a lambda expression that uses a local variable
max1. According to the error message, local variables referenced from a lambda expression must befinaloreffectively final.In Java, a variable is considered
effectively finalif its value is not modified after it is initialized. In your case, to fix the error, you can either declaremax1asfinalor ensure that it is effectively final by not modifying its value after initialization.Here's an example of how you can modify the code to make the variable effectively final:
int max1 = ... // initialize max1 int freq1 = (int) Arrays.stream(rank) .filter(a -> a == max1) .count();If you need to modify the value of
max1after initialization, you should declare it asfinal:final int max1 = ... // initialize max1 int freq1 = (int) Arrays.stream(rank) .filter(a -> a == max1) .count();Note that if you declare
max1as , you won't be able to modify its value further in the code.