Java 8流forEach

时间:2015-10-11 04:39:31

标签: 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

它不允许我这样做。我所能拥有的只是&#39; e&#39;在print命令中,它打印对象的引用号。

感谢任何帮助:)

1 个答案:

答案 0 :(得分:6)

让我们看看编译器说的是什么。 (如果您提供了minimal, complete and verifiable example,这会更容易。)

$ 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是“原始”。 Reading up on generics告诉我们,我们应该指定要放入列表的对象类型。所以请设置它。

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()));
    }
}

现在它有效!

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