0

My code is:

    int count = 0;
    contactVisits.keySet().forEach(
            key -> {
                count++; //cannot reach count here 

                //.doing my stuff......

                // ..
            }
    );

i need count in lambda, counting with "count" variable but netbeans telling me a problem like "local variables referenced from a lambda expression must be final or effectively final"

Is there a way to get index count of map in Lambda expression or getting a variable outside ?

2
  • What do you mean by index count of a Map where there's no such thing called index in a map. Commented Oct 4, 2018 at 7:42
  • how can i call it? i like to fix my question. Commented Oct 4, 2018 at 8:06

1 Answer 1

1

Best way is just use size() method on your map.

You may also use atomicinteger instead of int, and call getAndIncremend instead count++, however its not effective.

There is also stream().count() but i think first approach is the best.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.