1 回答

TA貢獻1993條經驗 獲得超6個贊
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());
添加回答
舉報