Last active
December 20, 2015 07:59
-
-
Save lucaspolo/6097262 to your computer and use it in GitHub Desktop.
Brincando um pouco com Lambda Function no Java
This file contains 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
import java.util.List; | |
import java.util.ArrayList; | |
//This interface apply a pre-condition | |
interface Predicate<T>{ | |
boolean test(T t); | |
} | |
//This interface apply a modification on element | |
interface Function<E, S> { | |
S apply(E e); | |
} | |
public Lambda Teste { | |
public static void main(String args[]) { | |
List<String> lista = new ArrayList<>(); | |
lista.add("Lucas"); | |
lista.add("Zezim"); | |
lista.add("Morpheu"); | |
printString(lista, t -> t.equals("Lucas") ); | |
printString(lista, t -> { | |
return t.equals("Morpheu"); | |
} | |
); | |
List<String> modified = applicator(lista, p -> p + "s"); | |
for(String word : modified) { | |
System.out.println(word); | |
} | |
} | |
public static void printString(List<String> a, Predicate<String> tester) { | |
for(String word : a) { | |
if(tester.test(word)) { | |
System.out.println(word); | |
} | |
} | |
} | |
public static List<String> applicator(List<String> a, Function<String, String> function) { | |
List<String> b = new ArrayList<>(); | |
for(String word : a) { | |
b.add(function.apply(word)); | |
} | |
return b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment