Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6f33def

Browse files
Challenge 60.
reduce stream operation. AllMatch method.
1 parent 460d05e commit 6f33def

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package challenge51_60;
2+
3+
import java.util.List;
4+
import java.util.function.Predicate;
5+
6+
/**
7+
* stream reduce operation.
8+
* filter and allmatch regarding the intermediate operation behaviour.
9+
* // TODO: 04/12/2019 understand all the overloaded reduce methods.
10+
*
11+
* Optional<T> reduce(BinaryOperator<T> accumulator)
12+
* T reduce(T identity, BinaryOperator<T> accumulator)
13+
*
14+
* Identity : initial value of the reduction, and in the same time
15+
* a default result if the stream is empty.
16+
*
17+
* Accumulator : A function that takes two parameters, BinaryOperator<T> extends BiFunction<T,T,T>
18+
* the two parameters are : the partial result of the reduction operation
19+
* and the next element of the stream.
20+
*/
21+
public class Challenge_60 {
22+
public static void main( String[] args ) {
23+
List<String> list = List.of("Neo", "Morpheus", "Oracle", "Trinity", "Neo");
24+
Predicate<String> neoSearch = str -> {
25+
System.out.println("Agent Smith is looking for Neo...");
26+
return str.contains("Neo");
27+
};
28+
var binaryNumbers = List.of(1, 0, 1, 1).stream();
29+
Integer binarySum = binaryNumbers.reduce(Integer::sum).orElseThrow(StackOverflowError::new);
30+
31+
boolean neoFound = list.stream().filter( s -> s.length() >= binarySum).allMatch(neoSearch);
32+
System.out.println(neoFound);
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /