所以我试图仅使用Stream来计算ArrayList中的对象数.我正在使用stream.count但是只返回1,因为我的列表有多个对象.这是一些代码:
static ArrayList<Person> people = new ArrayList<>();
void loadCourses(){ // putting Person objects in the list }
public void countPeople()
{
Stream<ArrayList<Person>> stream1 = Stream.of(people);
long l = stream1.count();
System.out.println("Number of people loaded: " + l);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏:)
我试图迭代对象的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',它打印对象的参考号.
任何帮助表示赞赏:)