This gist contains all code snippets from the article.
snippet-3.java
snippet-7.java
snippet-10.java
snippet-2.java
List<Integer> runningMax = values.stream() | |
.gather(Gatherers.scan( | |
() -> Integer.MIN_VALUE, // 1️⃣ Start with smallest possible value | |
Integer::max // 2️⃣ Keep the maximum at each step | |
)) | |
.toList(); | |
System.out.println("Running maximum: " + runningMax); | |
// Output: [-2147483648, 1, 5, 5, 8, 8, 9, 9, 9, 9] | |
// ↑ initial ↑ 5>1 ↑ 8>5 ↑ 9>8 |
List<Integer> runningMax = values.stream() | |
.gather(Gatherers.scan( | |
() -> Integer.MIN_VALUE, | |
Integer::max | |
)) | |
.toList(); | |
System.out.println("Running maximum: " + runningMax); | |
// Output: [-2147483648, 1, 5, 5, 8, 8, 9, 9, 9, 9] | |
// ↑ initial ↑ 5>1 ↑ 8>5 ↑ 9>8 |
List<Integer> runningMax = values.stream() | |
.gather(Gatherers.scan( | |
() -> Integer.MIN_VALUE, | |
Integer::max | |
)) | |
.toList(); | |
System.out.println("Running maximum: " + runningMax); | |
// Output: [-2147483648, 1, 5, 5, 8, 8, 9, 9, 9, 9] | |
// ↑ initial ↑ 5>1 ↑ 8>5 ↑ 9>8 |
record PeakValley(String type, double value, int index) {} | |
public static Gatherer<Double, ?, PeakValley> peakValleyDetection() { | |
return Gatherer.of( | |
() -> new Object() { | |
Double prev = null; // 1️⃣ Previous value | |
Double current = null; // 2️⃣ Current value | |
int index = 0; // 3️⃣ Track position | |
}, | |
(state, next, downstream) -> { |