teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

reduse,andを使わない書き方

2018/08/11 03:40

投稿

swordone
swordone

スコア20675

answer CHANGED
@@ -5,6 +5,17 @@
5
5
  return Stream.of(conditions).reduce(t -> true, Predicate::and);
6
6
  }
7
7
  ```
8
+ もしくはandなどを使わず、こう書く手段もある。
9
+ ```java
10
+ public static <T> Predicate<T> satisfyAll(Predicate<T>... conditions) {
11
+ return x -> {
12
+ for (Predicate<T> p : conditions) {
13
+ if(!p.test(x)) return false;
14
+ }
15
+ return true;
16
+ };
17
+ }
18
+ ```
8
19
  で、こう使う。
9
20
  ```java
10
21
  List<Test> testList = inputStream.stream()

1

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

2018/08/11 03:40

投稿

swordone
swordone

スコア20675

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  こういうメソッドを用意すればいいのでは。
2
2
  ```java
3
3
  // 渡した条件全てに合致したときtrueを返すPredicateを作成
4
- public static <T> Predicate satisfyAll(Predicate<T>... conditions) {
4
+ public static <T> Predicate<T> satisfyAll(Predicate<T>... conditions) {
5
5
  return Stream.of(conditions).reduce(t -> true, Predicate::and);
6
6
  }
7
7
  ```