Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save disc99/f365cb1b32a4913e9000 to your computer and use it in GitHub Desktop.
Save disc99/f365cb1b32a4913e9000 to your computer and use it in GitHub Desktop.
@Test
public void Functionのdefaultメソッド() throws Exception {
Function<String, String> s1 = s -> s + "1";
Function<String, String> s2 = s -> s + "2";
Function<String, String> s3 = s -> s + "3";
// andThen
String res1 = s1.andThen(s2).andThen(s3).apply("start:");
assertThat(res1, is("start:123"));
// compose
String res2 = s1.compose(s2).compose(s3).apply("start:");
assertThat(res2, is("start:321"));
// identity
Function<String, String> f1 = Function.identity();
Function<String, String> f2 = s -> s;
String actual = f1.apply("test");
String expected = f2.apply("test");
assertThat(actual, is(expected));
}
@Test
public void Consumerのdefaultメソッド() throws Exception {
Consumer<String> s1 = System.out::print;
Consumer<String> s2 = System.out::print;
// result: xx
s1.andThen(s2).accept("x");
}
@Test
public void Predicateのdefaultメソッド() throws Exception {
Predicate<String> p1 = s -> s.length() > 1;
Predicate<String> p2 = s -> s.length() < 10;
new ArrayList<String>().stream()
.filter(p1.and(p2)) // 1 < param && param < 10
.filter(p1.or(p2)) // 1 < param || param < 10
.filter(p1.negate()) // !(1 < param)
.filter(isEqual("test")) // "test".equals(param)
.filter(isEqual(null)) // param == null
.collect(toList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment