title: Variable is already defined in method lambda
body: <p>Consider the following <em>almost</em> compilable Java 8 code:</p>
  
  <pre><code>public static void main(String[] args) {
  
      LinkedList&lt;User&gt; users = null;
      users.add(new User(1, "User1"));
      users.add(new User(2, "User2"));
      users.add(new User(3, "User3"));
  
      User user = users.stream().filter((user) -&gt; user.getId() == 1).findAny().get();
  }
  
  static class User {
  
      int id;
      String username;
  
      public User() {
      }
  
      public User(int id, String username) {
          this.id = id;
          this.username = username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getUsername() {
          return username;
      }
  
      public int getId() {
          return id;
      }
  }
  </code></pre>
  
  <p>You'll notice <code>User user = users.stream().filter((user) -&gt; user.getId() == 1).findAny().get();</code> throws a compiler error:</p>
  
  <blockquote>
    <p>variable user is already defined in method main(String[])</p>
  </blockquote>
  
  <p>My question is: Why do Lambda expressions consider the variable that is <em>being initialized</em> on the same line as the Lambda expression as already defined? I understand Lambdas look outside themselves for (and use) local variables, so you can't name the variables you use inside the Lambda the same as an outside variable. But why is the variable that is <em>being</em> defined considered <em>already</em> defined?</p>