变量已经在方法lambda中定义 [英] Variable is already defined in method lambda

查看:119
本文介绍了变量已经在方法lambda中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下几乎可编译的Java 8代码:

Consider the following almost compilable Java 8 code:

public static void main(String[] args) {

    LinkedList<User> 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) -> 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;
    }
}

您会注意到用户user = users.stream()。filter((user) - > user.getId()== 1).findAny()。get(); 抛出编译器错误:

You'll notice User user = users.stream().filter((user) -> user.getId() == 1).findAny().get(); throws a compiler error:


变量用户已在方法main中定义(String [])

variable user is already defined in method main(String[])

我的问题是:为什么Lambda表达式认为被初始化的变量与已定义的Lambda表达式在同一行?我理解Lambdas在外部查找(并使用)局部变量,因此您不能将Lambda中使用的变量命名为外部变量。但是为什么 定义的变量被认为已经定义?

My question is: Why do Lambda expressions consider the variable that is being initialized 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 being defined considered already defined?

推荐答案

<让我们转到名称及其范围的Java语言规范


方法形式参数的范围(§8.4.1),构造函数
(§8.8) .1),或lambda表达式(§15.27)是
方法,构造函数或lambda表达式的整个主体。

The scope of a formal parameter of a method (§8.4.1), constructor (§8.8.1), or lambda expression (§15.27) is the entire body of the method, constructor, or lambda expression.

范围一个块(第14.4节)中的局部变量声明是声明出现的块的
其余部分
,从其
自己的初始值设定项开始,并包括任何进一步的声明符。在
右边的本地变量声明声明。

然后,关于shado翼和模糊


局部变量(§14.4),形式参数(§8.4.1,§15.27.1) ,
异常参数(§14.20),本地类(§14.3)只能使用简单名称引用
,而不是限定名称(§6.2)。

A local variable (§14.4), formal parameter (§8.4.1, §15.27.1), exception parameter (§14.20), and local class (§14.3) can only be referred to using a simple name, not a qualified name (§6.2).

在本地
变量,形式参数,异常参数或本地类
声明的范围内,不允许某些声明,因为无法区分
仅使用简单名称声明实体。

Some declarations are not permitted within the scope of a local variable, formal parameter, exception parameter, or local class declaration because it would be impossible to distinguish between the declared entities using only simple names.

如果使用
声明一个局部变量v的名称,则为编译时错误v的范围内的新变量,除非新的
变量是在声明在v的
范围内的类中声明的。

所以,在

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

,变量 user 的范围是在那个街区之后的一切。现在您尝试使用该变量的名称在范围内声明一个新变量,但不是

, the scope of the variable user is everything after it in that block. Now you are trying to use the name of that variable to declare a new variable within the scope, but not


在一个类中其声明属于v。

的范围,因此发生编译时错误。 (它在lambda表达式中声明,而不是在类中声明。)

so a compile time error occurs. (It's declared in a lambda expression, not in a class.)

这篇关于变量已经在方法lambda中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆