Java 8 forEach returning property from inside loop

Viewed 844

I need to return a property of a certain object from which I need to access within a forEach loop. Basically I have a user object that that has a property of List<UserLocation> and inside the UserLocation object is a Location object with a property of location_id. If the store_id on the user object matches the store_id on the UserLocation object, that is the one I need to get the location_id from. However, the issue I am getting is that it says the variable used inside the lambda expression should be final or effectively final. See code below.

User user = getUser(request);
Integer locationId;

user.getUserLocations().forEach(ul -> {
    if (ul.getStoreId() == user.getStoreId()) {
        locationId= ul.getUserLocations().getLocationId();
    }
});

Any advice or solutions would be appreciated!

2 Answers
Related