Index: web-module/src/main/java/egovframework/com/ext/jstree/support/util/Java8Lambda.java =================================================================== diff -u -r366cf516bedd4538eb63dbff96de1ffc659d30ba -ra7f1051aa1b1652f0909d43c40a44e602ef9a9b0 --- web-module/src/main/java/egovframework/com/ext/jstree/support/util/Java8Lambda.java (.../Java8Lambda.java) (revision 366cf516bedd4538eb63dbff96de1ffc659d30ba) +++ web-module/src/main/java/egovframework/com/ext/jstree/support/util/Java8Lambda.java (.../Java8Lambda.java) (revision a7f1051aa1b1652f0909d43c40a44e602ef9a9b0) @@ -5,10 +5,16 @@ */ public class Java8Lambda { - private String givenName; - private String surName; - private int age; - private String eMail; - private String phone; - private String address; + String name; + int age; + + Java8Lambda(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public String toString() { + return name; + } } Index: web-module/src/test/java/egovframework/com/ext/jstree/support/util/Java8LambdaTest.java =================================================================== diff -u -r439e35563715460ed013f7ff95433cb7da347da8 -ra7f1051aa1b1652f0909d43c40a44e602ef9a9b0 --- web-module/src/test/java/egovframework/com/ext/jstree/support/util/Java8LambdaTest.java (.../Java8LambdaTest.java) (revision 439e35563715460ed013f7ff95433cb7da347da8) +++ web-module/src/test/java/egovframework/com/ext/jstree/support/util/Java8LambdaTest.java (.../Java8LambdaTest.java) (revision a7f1051aa1b1652f0909d43c40a44e602ef9a9b0) @@ -1,9 +1,11 @@ package egovframework.com.ext.jstree.support.util; +import org.junit.BeforeClass; import org.junit.Test; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -12,6 +14,19 @@ */ public class Java8LambdaTest { + static List persons; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + persons = + Arrays.asList( + new Java8Lambda("Max", 18), + new Java8Lambda("Peter", 23), + new Java8Lambda("Pamela", 23), + new Java8Lambda("David", 12)); + } + + @Test public void functionalJavaTest1() { System.out.println("=== RunnableTest ==="); @@ -60,17 +75,108 @@ } @Test - public void functionJavaIntStreamTest(){ + public void functionalJavaIntStreamTest(){ IntStream.range(1, 4) .forEach(System.out::println); } @Test - public void functionJavaPridicateTest(){ + public void functionalJavaPridicateTest(){ Arrays.stream(new int[] {1, 2, 3}) .map(n -> 2 * n + 1) .average() .ifPresent(System.out::println); } + @Test + public void functionalJavaConvertStreamTest(){ + Stream.of("a1", "a2", "a3") + .map(s -> s.substring(1)) + .mapToInt(Integer::parseInt) + .max() + .ifPresent(System.out::println); + } + + @Test + public void functionalJavaConvertObjTest(){ + IntStream.range(1, 4) + .mapToObj(i -> "a" + i) + .forEach(System.out::println); + } + + @Test + public void functionalJavaConvertInteractiveTest(){ + Stream.of(1.0, 2.0, 3.0) + .mapToInt(Double::intValue) + .mapToObj(i -> "a" + i) + .forEach(System.out::println); + } + + @Test + public void functionalJavaFilterTest(){ + Stream.of("d2", "a2", "b1", "b3", "c") + .filter(s -> { System.out.println("filter: " + s); return true; }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + @Test + public void functionalJavaMapTest(){ + Stream.of("d2", "a2", "b1", "b3", "c") + .map(s -> { System.out.println("map: " + s); return s.toUpperCase(); }) + .anyMatch(s -> { System.out.println("anyMatch: " + s); return s.startsWith("A"); }); + } + + @Test + public void functionalJavaMapFilterMixTest(){ + Stream.of("d2", "a2", "b1", "b3", "c") + .map(s -> { System.out.println("map: " + s); return s.toUpperCase(); }) + .filter(s -> { System.out.println("filter: " + s); return s.startsWith("A"); }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + @Test + public void functionalJavaMapFilterOrderTest(){ + Stream.of("d2", "a2", "b1", "b3", "c") + .filter(s -> { System.out.println("filter: " + s); return s.startsWith("a"); }) + .map(s -> { System.out.println("map: " + s); return s.toUpperCase(); }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + @Test + public void functionalJavaMapFilterSortMixTest(){ + Stream.of("d2", "a2", "b1", "b3", "c") + .sorted((s1, s2) -> { System.out.printf("sort: %s; %s\n", s1, s2); return s1.compareTo(s2); }) + .filter(s -> { System.out.println("filter: " + s); return s.startsWith("a"); }) + .map(s -> { System.out.println("map: " + s); return s.toUpperCase(); }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + @Test + public void functionalJavaMapFilterSortPerformanceTest(){ + Stream.of("d2", "a2", "b1", "b3", "c") + .filter(s -> { + System.out.println("filter: " + s); + return s.startsWith("a"); + }) + .sorted((s1, s2) -> { + System.out.printf("sort: %s; %s\n", s1, s2); + return s1.compareTo(s2); + }) + .map(s -> { + System.out.println("map: " + s); + return s.toUpperCase(); + }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + @Test + public void functionalJavaCollectTest(){ + List filtered = + persons + .stream() + .filter(p -> p.name.startsWith("P")) + .collect(Collectors.toList()); + + System.out.println(filtered); // [Peter, Pamela] + } }