Last active
June 6, 2024 16:58
-
-
Save jeanbza/56a6aa6e99c6a3175aed8fb64a3ef748 to your computer and use it in GitHub Desktop.
lambdas vs functions in streams
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
// This is nice and terse. | |
public static void main(String[] args){ | |
ArrayList<Pair<String, int>> test_scores = {{"Corey", 79}, {"Jason", 54}, {"Alice", 92}, {"Mark", 68}}; | |
test_scores.stream() | |
.filter(s -> s.getValue() > 80) | |
.forEach(s -> System.out.println(s.getKey() + " passed the test")); | |
} | |
// This is annoyingly verbose. | |
public static void main(String[] args){ | |
ArrayList<Pair<String, int>> test_scores = {{"Corey", 79}, {"Jason", 54}, {"Alice", 92}, {"Mark", 68}}; | |
test_scores.stream() | |
.filter(passedTheTest) | |
.forEach(printPassed); | |
} | |
public boolean passedTheTest(Pair<String, int> s) { | |
return s.getValue() > 70 | |
} | |
public boolean printPassed(Pair<String, int> s) { | |
System.out.println(s.getKey() + " passed the test") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment