1 回答
TA贡献1993条经验 获得超5个赞
personList是一个人的名单
Collections.singletonList(personList)是人员列表
objects是人员/地点列表的列表。
List<Object> persons = objects.get(0); // persons is a List of List of Person
List<String> firstNames = persons.stream()
//each element in the stream is a List of Person, so you cannot cast it to Person.
.map(o -> ((Person)o).getFirstName())
.collect(Collectors.toList());
您可以删除 singletonList 函数,以便减少列表级别:
List<List<?>> objects = new ArrayList<>();
objects.add(personList);
objects.add(placeList);
或者在做地图时更深入地列出一个列表:
List<String> firstNames = persons.stream()
//Because persons is declared as List of Objects, so you need to cast each element into a List before calling get
.map(o -> ((Person)((List)o).get(0))).getFirstName())
.collect(Collectors.toList());
添加回答
举报