Last active
August 30, 2023 04:49
-
-
Save JonasGao/957514a3b1ab32e5378b543f5755fb3b to your computer and use it in GitHub Desktop.
Test the same object add to set
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
public static void main(String[] args) { | |
SomeClass1 a1 = new SomeClass1(); | |
a1.setName("A"); | |
SomeClass1 a2 = new SomeClass1(); | |
a2.setName("A"); | |
SomeClass1 b = new SomeClass1(); | |
b.setName("B"); | |
SomeClass1 c = new SomeClass1(); | |
c.setName("C"); | |
Map<String, SomeClass1> map = new HashMap<>(); | |
map.put(a1.getName(), a1); | |
map.put(a2.getName(), a2); | |
map.put(b.getName(), b); | |
map.put(c.getName(), c); | |
List<String> stringList = Arrays.asList("A", "A", "B", "A", "B", "C", "B", "C", "A", "B"); | |
final Map<String, Set<SomeClass1>> collect = stringList.stream() | |
.collect( | |
groupingBy( | |
Function.identity(), | |
mapping( | |
map::get, | |
toSet() | |
) | |
) | |
); | |
System.out.println(collect); | |
System.out.println(collect.get("A").getClass()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment