Skip to content

Instantly share code, notes, and snippets.

@kidinov
Created February 20, 2017 22:00
Show Gist options
  • Select an option

  • Save kidinov/39daa2fa0beedc0261730505f3799b0a to your computer and use it in GitHub Desktop.

Select an option

Save kidinov/39daa2fa0beedc0261730505f3799b0a to your computer and use it in GitHub Desktop.
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