-
-
Save mauricio/924838 to your computer and use it in GitHub Desktop.
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.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
class Tupla <T> { | |
private final T esquerda; | |
private final T direita; | |
public Tupla(T esquerda, T direita) { | |
this.esquerda = esquerda; | |
this.direita = direita; | |
} | |
public T getDireita() { | |
return direita; | |
} | |
public T getEsquerda() { | |
return esquerda; | |
} | |
} | |
interface MapFunction <IN,OUT> { | |
public OUT map( IN tupla ); | |
} | |
class Collections { | |
public static final <IN,OUT> List<OUT> map( List<IN> tuplas, MapFunction<IN,OUT> function ) { | |
List<OUT> resultados = new LinkedList<OUT>(); | |
for ( IN elemento : tuplas ) { | |
resultados.add( function.map(elemento) ); | |
} | |
return resultados; | |
} | |
} | |
public class Trolling { | |
public static void main(String[] args) { | |
List<Tupla<Integer>> tuplas = Arrays.asList( | |
new Tupla<Integer>( 1, 2 ), | |
new Tupla<Integer>( 3, 4 ), | |
new Tupla<Integer>( 5, 6 ) | |
); | |
List<Integer> resultado = Collections.map(tuplas, new MapFunction<Tupla<Integer>,Integer>() { | |
@Override | |
public Integer map(Tupla<Integer> tupla) { | |
return tupla.getEsquerda() + tupla.getDireita(); | |
} | |
}); | |
System.out.printf( "Resultado -> %s%n", resultado ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment