1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import java.util.*;
interface F<A,B>{
B app(A a);
}
class Map {
static <A,B> LinkedList<B> map(F<A,B> f, LinkedList<A> l){
LinkedList<B> l2= new LinkedList<B>();
for (A e : l)
l2.add(f.app(e));
return l2;
}
}
public class Test{
public static void main(String[] a)
{
LinkedList<Integer> l = new LinkedList<Integer>();
l.add(1);
l.add(2);
l.add(3);
l.add(4);
LinkedList<Integer> l2 = Map.map(new F<Integer,Integer>(){
public Integer app(Integer a){return a * a;}}, l);
System.out.println(l);
System.out.println(l2);
}
} |
Partager