Variable is already defined in method lambda

后端 未结 5 902
执念已碎
执念已碎 2020-11-30 08:59

Consider the following almost compilable Java 8 code:

public static void main(String[] args) {

    LinkedList users = null;
    users.a         


        
5条回答
  •  悲哀的现实
    2020-11-30 09:41

    look at the code

    User user = users.stream().filter((user) -> user.getId() == 1).findAny().get();
    

    The variable name is user and the variable inside the lambda is also user

    try changing it to be something like this

    User user = users.stream().filter((otherUser) -> otherUser.getId() == 1).findAny().get();
    

提交回复
热议问题