Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

How to join nested collections with Stream API

For example, in PushOperation.java in EGit, PushOperationSpecification contains LinkedHashMap<URIish, Collection<RemoteRefUpdate>>. So you can join all remote name with Stream API like:

String specs = this.specification.getURIs().stream()
        .map(uri -> this.specification.getRefUpdates(uri))
        .collect(StringBuilder::new,
                (sb, refupdates) -> sb.append(refupdates.stream()
                        .map(remote -> remote.getRemoteName())
                        .collect(Collectors.joining(","))),
                (sb1, sb2) -> sb1.append(sb2))
        .toString();