Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Last active June 23, 2023 10:33
Show Gist options
  • Save saswata-dutta/5db8b9fa0501411e2d21882f1ee4e5db to your computer and use it in GitHub Desktop.
Save saswata-dutta/5db8b9fa0501411e2d21882f1ee4e5db to your computer and use it in GitHub Desktop.
import java.util.*;
import java.util.stream.Collectors;
class Main {
static record Record(String asin, String sid, String wid) {}
public static void main(String[] args) {
var m =
Map.ofEntries(
Map.entry("k1", List.of(new Record("a1", "s1", "w1"), new Record("a2", "s1", "w1"))),
Map.entry("k2", List.of(new Record("a1", "s2", "w1"), new Record("a2", "s2", "w1"))),
Map.entry("k3", List.of(new Record("a1", "s3", "w1"), new Record("a2", "s3", "w1")))
);
var s = m.values().stream().
flatMap(Collection::stream).
map(Record::asin).
collect(Collectors.toSet());
System.out.println(s);
}
}
import java.util.List;
import java.util.stream.Collectors;
class Main {
static record Record(String asin, String sid, String wid) {}
public static void main(String[] args) {
var l = List.of(
new Record("a1", "s1", "w1"),
new Record("a1", "s1", "w2"),
new Record("a1", "s1", "w3"),
new Record("a1", "s2", "w1"),
new Record("a1", "s2", "w2"),
new Record("a1", "s2", "w3"),
new Record("a2", "s1", "w1"),
new Record("a2", "s1", "w2"),
new Record("a2", "s1", "w3"),
new Record("a2", "s2", "w1"),
new Record("a2", "s2", "w2"),
new Record("a2", "s2", "w3")
);
var grp = l.stream()
.collect(
Collectors.groupingBy(
Record::sid,
Collectors.groupingBy(
Record::wid,
Collectors.mapping(Record::asin, Collectors.toSet())
)
)
);
System.out.println(grp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment