Skip to content

Instantly share code, notes, and snippets.

@skht777
Last active February 4, 2017 17:44
Show Gist options
  • Select an option

  • Save skht777/7321a75c196bf07a2e5ce73f5471a454 to your computer and use it in GitHub Desktop.

Select an option

Save skht777/7321a75c196bf07a2e5ce73f5471a454 to your computer and use it in GitHub Desktop.
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MyCollectors {
static class Struct {
private String val1, val2, val3;
Struct(String val1, String val2, String val3) {
this.val1 = val1;
this.val2 = val2;
this.val3 = val3;
}
public String getVal1() {
return val1;
}
public String getVal2() {
return val2;
}
public String getVal3() {
return val3;
}
@Override
public String toString() {
return Stream.of(getVal1(), getVal2(), getVal3()).collect(Collectors.joining(", ", "(", ")"));
}
}
public static void main(String[] args) {
Struct s1 = new Struct("apple", "tokyo", "hoge");
Struct s2 = new Struct("apple", "tokyo", "foo");
Struct s3 = new Struct("apple", "orange", "bar");
Map<Boolean, List<Struct>> map = Stream.of(s1, s2, s3).collect(Collectors.partitioningBy(s -> s.getVal1().equals("apple") && s.getVal2().equals("tokyo")));
System.out.println(map.get(true)); // [(apple, tokyo, hoge), (apple, tokyo, foo)]
System.out.println(map.get(false)); // [(apple, orange, bar)]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment