Created
February 20, 2017 22:00
-
-
Save kidinov/39daa2fa0beedc0261730505f3799b0a 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.Arrays; | |
| import java.util.Collection; | |
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| public class Main { | |
| public static class Thingy { | |
| private DifferentThingy nestedThingy; | |
| public Thingy(DifferentThingy nestedThingy) { | |
| this.nestedThingy = nestedThingy; | |
| } | |
| private DifferentThingy getNestedThingy() { | |
| return this.nestedThingy; | |
| } | |
| } | |
| public static class DifferentThingy { | |
| private String attr; | |
| public DifferentThingy(String attr) { | |
| this.attr = attr; | |
| } | |
| private String getAttr() { | |
| return this.attr; | |
| } | |
| } | |
| public static void main(String... args) { | |
| getUniqueBasedOnDifferentThingyAttr(Arrays.asList(new Thingy(new DifferentThingy("1")), | |
| new Thingy(new DifferentThingy("1")), | |
| new Thingy(new DifferentThingy("1")), | |
| new Thingy(new DifferentThingy("2")), | |
| new Thingy(new DifferentThingy("3")), | |
| new Thingy(new DifferentThingy("1")))); | |
| } | |
| private static Collection<Thingy> getUniqueBasedOnDifferentThingyAttr(List<Thingy> originalList) { | |
| return originalList.stream() | |
| .collect(Collectors.toMap(thing -> thing.getNestedThingy().getAttr(), p -> p, (p, q) -> p)) | |
| .values(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment