Created
March 13, 2013 07:47
-
-
Save ooharak/5150063 to your computer and use it in GitHub Desktop.
Java8 Lambda example
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.*; | |
| import java.util.function.Block; | |
| import java.util.function.Function; | |
| /** | |
| * Created with IntelliJ IDEA. | |
| * User: Administrator | |
| * Date: 13/03/13 | |
| * Time: 16:04 | |
| * To change this template use File | Settings | File Templates. | |
| */ | |
| public class LambdaExample { | |
| static interface MyInterface { | |
| void yield(String s); | |
| } | |
| private static void doForAll(MyInterface mi) { | |
| for (String s : new String[]{"aaa", "bbb", "ccc"}) { | |
| mi.yield(s); | |
| } | |
| } | |
| private static <T, M> List<M> map(Function<T, M> f, Collection<T> collection) { | |
| Iterator<T> i = collection.iterator(); | |
| ArrayList<M> list = new ArrayList<>(); | |
| while (i.hasNext()) { | |
| T t = i.next(); | |
| list.add(f.apply(t)); | |
| } | |
| return list; | |
| } | |
| public static void main(String[] args) { | |
| doForAll((n) -> System.out.println(n)); | |
| doForAll(System.out::println); | |
| System.out.println(map((s) -> "<" + s + ">", Arrays.asList("ppp", "qqq", "rrr"))); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment