Created
February 12, 2014 05:17
-
-
Save firstspring1845/8950420 to your computer and use it in GitHub Desktop.
mapっぽいの
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
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.List; | |
public class Util { | |
public static <T, E> List<E> map(MapFunction<T, E> f, Iterable<T> i) { | |
ArrayList<E> l = new ArrayList<>(); | |
Iterator<T> it = i.iterator(); | |
while (it.hasNext()) { | |
l.add(f.apply(it.next())); | |
} | |
return l; | |
} | |
public static <E> List<E> asList(Iterator<E> i) { | |
ArrayList<E> l = new ArrayList<>(); | |
while (i.hasNext()) { | |
l.add(i.next()); | |
} | |
return l; | |
} | |
public static interface MapFunction<T, E> { | |
public E apply(T o); | |
} | |
public static void main(String[] args) { | |
List<Integer> l = new ArrayList(); | |
for (int i = 0; i < 100; i++) { | |
l.add(i); | |
} | |
List<String> ll = Util.map(new Util.MapFunction<Integer, String>() { | |
public String apply(Integer o) { | |
return o.toString(); | |
} | |
}, l); | |
for (String string : ll) { | |
System.out.println(string); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment