回答編集履歴

2

reduse,andを使わない書き方

2018/08/11 03:40

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -7,6 +7,28 @@
7
7
  public static <T> Predicate<T> satisfyAll(Predicate<T>... conditions) {
8
8
 
9
9
  return Stream.of(conditions).reduce(t -> true, Predicate::and);
10
+
11
+ }
12
+
13
+ ```
14
+
15
+ もしくはandなどを使わず、こう書く手段もある。
16
+
17
+ ```java
18
+
19
+ public static <T> Predicate<T> satisfyAll(Predicate<T>... conditions) {
20
+
21
+ return x -> {
22
+
23
+ for (Predicate<T> p : conditions) {
24
+
25
+ if(!p.test(x)) return false;
26
+
27
+ }
28
+
29
+ return true;
30
+
31
+ };
10
32
 
11
33
  }
12
34
 

1

Predicateにジェネリクスつけ忘れた

2018/08/11 03:40

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  // 渡した条件全てに合致したときtrueを返すPredicateを作成
6
6
 
7
- public static <T> Predicate satisfyAll(Predicate<T>... conditions) {
7
+ public static <T> Predicate<T> satisfyAll(Predicate<T>... conditions) {
8
8
 
9
9
  return Stream.of(conditions).reduce(t -> true, Predicate::and);
10
10