Last active
June 23, 2023 10:33
-
-
Save saswata-dutta/5db8b9fa0501411e2d21882f1ee4e5db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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