Last active
November 4, 2021 00:36
-
-
Save ElectroluxV2/7f4daba93d3ffc2f18abdfc046d11241 to your computer and use it in GitHub Desktop.
Java reduce but with interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stdout: [start, aa, bb, cc, d, dd]