Java 8流forEach

Ner*_*909 0 java java-8 java-stream

我试图迭代对象的ArrayList并打印出列表中每个对象的'name'属性.但是,即使我在Person类中有名称的getter方法,我也无法访问该对象的属性.这是一些代码:

 ArrayList persons = new ArrayList<>();
 persons.add(new Person("Name","Age"));
 persons.add(new Person("Name1","Age1"));
 persons.add(new Person("Name2","Age2"));

persons
 .stream()
 .forEach(e -> System.out.println(e.getName()));   //giving an error

  forEach(e -> System.out.println(e));  //no error
Run Code Online (Sandbox Code Playgroud)

它不允许我这样做.我只能在打印命令中使用'e',它打印对象的参考号.

任何帮助表示赞赏:)

5go*_*der 6

让我们看看编译器说的是什么.(如果您提供了一个最小,完整且可验证的示例,这会更容易.)

$ javac Main.java
Main.java:33: error: cannot find symbol
            .forEach(e -> System.out.println(e.getName()));
                                              ^
  symbol:   method getName()
  location: variable e of type Object
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

你收到警告和错误.错误说这e是类型Object- 正如我们所知 - 没有方法getName.这应该提醒你,因为毕竟你已经把类型的对象Person放在你的List,不是吗?警告说我们应该用-Xlint:unchecked旗子重新编译以获取详细信息.所以我们这样做吧.

$ javac -Xlint:unchecked Main.java
Main.java:28: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
        persons.add(new Person("Name1", 1));
                   ^
  where E is a type-variable:
    E extends Object declared in interface List
Main.java:29: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
        persons.add(new Person("Name2", 2));
                   ^
  where E is a type-variable:
    E extends Object declared in interface List
Main.java:30: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
        persons.add(new Person("Name3", 3));
                   ^
  where E is a type-variable:
    E extends Object declared in interface List
Main.java:33: warning: [unchecked] unchecked call to forEach(Consumergt;) as a member of the raw type Stream
            .forEach(e -> System.out.println(e.getName()));
                    ^
  where T is a type-variable:
    T extends Object declared in interface Stream
Main.java:33: error: cannot find symbol
            .forEach(e -> System.out.println(e.getName()));
                                              ^
  symbol:   method getName()
  location: variable e of type Object
1 error
4 warnings

显然,编译器抱怨persons"原始".阅读泛型告诉我们,我们应该指定要放入列表的对象类型.所以就这样做吧.

import java.util.ArrayList;
import java.util.List;

final class Person {

    private final String name;
    private final int age;

    public Person(final String name, final int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }
}


public final class Main {

    public static void main(final String[] args) {
        final List<Person> persons = new ArrayList<>();  // <--- here ---
        persons.add(new Person("Name1", 1));
        persons.add(new Person("Name2", 2));
        persons.add(new Person("Name3", 3));
        persons
            .stream()
            .forEach(e -> System.out.println(e.getName()));
    }
}
Run Code Online (Sandbox Code Playgroud)

现在它有效!

$ javac Main.java && java -cp . Main
Name1
Name2
Name3