InstitutionUserConnection

InstitutionUserConnection

我将从InstitutionUserConnection-列表中获得所有Institution-id(InstitutionUserConnection包含一个称为机构的对象)

有人知道Java 8如何运作吗?
这是我的尝试,但是不起作用:

final List<InstitutionUserConnection> institutionUserConnections = ...
final List<String> institutionIds = institutionUserConnections.forEach(institution -> institution.getId());


非常感谢

[编辑]

过去的时尚:

final List<String> institutionIds = new ArrayList<>();
for(final InstitutionUserConnection institutionUserConnection : institutionUserConnections) {
        institutionIds.add(institutionUserConnection.getInstitution().getId());
    }

最佳答案

尝试类似的东西:

final List<String> institutionIds = institutionUserConnections.stream()
    .map(InstitutionUserConnection::getInstitution)
    .map(Institution::getId)
    .collect(Collectors.toList());


您可以找到更多类似的示例。在Collectors类的Javadoc中。

10-05 23:03