Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created Aug 19, 2021
Embed
What would you like to do?
StreamEx reducingWithZero demo
import one.util.streamex.MoreCollectors;
import one.util.streamex.StreamEx;
import java.util.stream.Collector;
class Test {
enum MyLattice {
/*
TOP
/ \
V1 V2
\ /
BOTTOM
*/
TOP, V1, V2, BOTTOM;
MyLattice join(MyLattice other) {
if (this == BOTTOM) return other;
if (other == BOTTOM) return this;
if (this == other) return this;
return TOP;
}
static Collector<MyLattice, ?, MyLattice> joining() {
return MoreCollectors.reducingWithZero(
TOP, BOTTOM, MyLattice::join);
}
}
public static void main(String[] args) {
MyLattice result = StreamEx.of("BOTTOM", "V1", "V2", "V1", "V1", "TOP")
.peek(e -> System.out.println("Processing " + e))
.map(MyLattice::valueOf)
.collect(MyLattice.joining());
System.out.println("Result: " + result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment