Skip to content

Instantly share code, notes, and snippets.

@rinaldifonseca
Forked from mauricio/SomaPares.java
Created April 19, 2011 00:34
Show Gist options
  • Save rinaldifonseca/926589 to your computer and use it in GitHub Desktop.
Save rinaldifonseca/926589 to your computer and use it in GitHub Desktop.
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