Intellij is complaining about this code, not sure why
its saying "variable used in lambda expression should be final or effectively final" (about j), and gives the option to convert j to atomic. Im wondering what that means, and what atomic is. (K and V are generics, this class extends HashMap)
9 Replies
⌛
This post has been reserved for your question.
Hey @blockgoblin31! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Inside a lambda, you can't reassign a local variable that was declared outside of that lambda.
Here you're reassigning j in the lambda, despite it's a local variable declared in get(). That doesn't work
IntelliJ offers to replace with an AtomicInteger, because then you wouldn't need to reassign it. You would just need to call a method to update it.
That is, however, a hacky solution that shouldn't be used. In your case, that wouldn't even solve the entire problem
Indeed, you're trying to return from inside the lambda, which can't be done either
Your better bet is to get rid of the lambda altogether, and to iterate over the Map's entries instead.
Im not sure how to do that
(Come to think of it, the better solution is to stream the keys, skip i-1 of them, and return the first found in the rest)
something like this?
Yeah, it's even better
cool, thanks
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@501514065068294154>.