2 回答

TA貢獻(xiàn)1719條經(jīng)驗 獲得超6個贊
你可以這樣做:
Map<String, Set<String>> entryMaps = new LinkedHashMap<>();
groups.forEach(group ->
group.getEntries().forEach(entry ->
entryMaps.computeIfAbsent(
entry.getEntryId().toLowerCase(),
k -> new LinkedHashSet<>())
.add(group.getId())));
這會迭代組,然后是每個組的條目Map.computeIfAbsent,LinkedHashSet如果鍵不存在,則使用新的空條目放置條目,返回此空集或匹配該鍵的集。然后,組 id 被添加到這個返回的集合中。
注意:我使用 aSet而不是Listfor 值,以避免可能的重復(fù)。而LinkedHashMap和LinkedhashSet保證插入順序。

TA貢獻(xiàn)1825條經(jīng)驗 獲得超6個贊
像這樣的東西應(yīng)該可以工作,它需要制作某種中間元組對象:
list.stream()
.flatMap(group ->
group.getEntries.stream()
.map(entry -> new GroupEntry(group.getId(), entry.getEntryId()))
)
.collect(
Collectors.groupingBy(GroupEntry::getEntryId, Collectors.mapping(GroupEntry::getGroupId, Collectors.toList())));
添加回答
舉報