Skip to content

Instantly share code, notes, and snippets.

@ElectroluxV2
Last active November 4, 2021 00:36
Show Gist options
  • Save ElectroluxV2/7f4daba93d3ffc2f18abdfc046d11241 to your computer and use it in GitHub Desktop.
Save ElectroluxV2/7f4daba93d3ffc2f18abdfc046d11241 to your computer and use it in GitHub Desktop.
Java reduce but with interface
package org.example;
import java.util.List;
import java.util.stream.Stream;
interface StringFilter {
List<String> filter(List<String> people);
default StringFilter and(StringFilter second) {
return list -> second.filter(this.filter(list));
}
}
public class App {
public static void main(String[] args){
StringFilter a = people -> people.stream().filter(s -> !s.equals("a")).toList();
StringFilter b = people -> people.stream().filter(s -> !s.equals("b")).toList();
StringFilter c = people -> people.stream().filter(s -> !s.equals("c")).toList();
// StringFilter d = people -> people.stream().filter(s -> !s.equals("d")).toList();
List<String> result = Stream.of(a, b, c)
.reduce(StringFilter::and).get().filter(List.of("start", "a", "aa", "b", "bb", "c", "cc", "d", "dd"));
System.out.println(result);
}
}
@ElectroluxV2
Copy link
Author

Stdout: [start, aa, bb, cc, d, dd]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment