1 回答

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
personList是一個(gè)人的名單
Collections.singletonList(personList)是人員列表
objects是人員/地點(diǎn)列表的列表。
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 函數(shù),以便減少列表級(jí)別:
List<List<?>> objects = new ArrayList<>();
objects.add(personList);
objects.add(placeList);
或者在做地圖時(shí)更深入地列出一個(gè)列表:
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());
添加回答
舉報(bào)