diff --git a/.project b/.project new file mode 100644 index 0000000..87154f5 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + java8-lambda-stream-insta + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/collection-interfaces/.classpath b/collection-interfaces/.classpath new file mode 100644 index 0000000..af1430b --- /dev/null +++ b/collection-interfaces/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/collection-interfaces/.project b/collection-interfaces/.project new file mode 100644 index 0000000..8e5eccf --- /dev/null +++ b/collection-interfaces/.project @@ -0,0 +1,23 @@ + + + collection-interfaces + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/collection-interfaces/.settings/org.eclipse.jdt.core.prefs b/collection-interfaces/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..714351a --- /dev/null +++ b/collection-interfaces/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/collection-interfaces/bin/.project b/collection-interfaces/bin/.project new file mode 100644 index 0000000..8e5eccf --- /dev/null +++ b/collection-interfaces/bin/.project @@ -0,0 +1,23 @@ + + + collection-interfaces + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/collection-interfaces/bin/pom.xml b/collection-interfaces/bin/pom.xml new file mode 100644 index 0000000..02ce465 --- /dev/null +++ b/collection-interfaces/bin/pom.xml @@ -0,0 +1,20 @@ + + + + java8-lambda-stream-insta + java8-lambda-stream-insta + 2.1 + + 4.0.0 + + collection-interfaces + + + + java8-lambda-stream-insta + common-test-tool + + + diff --git a/collection-interfaces/bin/src/test/java/collection/interfaces/Exercise1Test.class b/collection-interfaces/bin/src/test/java/collection/interfaces/Exercise1Test.class new file mode 100644 index 0000000..f8a2fc8 Binary files /dev/null and b/collection-interfaces/bin/src/test/java/collection/interfaces/Exercise1Test.class differ diff --git a/collection-interfaces/bin/src/test/java/collection/interfaces/Exercise2Test.class b/collection-interfaces/bin/src/test/java/collection/interfaces/Exercise2Test.class new file mode 100644 index 0000000..8d97c1d Binary files /dev/null and b/collection-interfaces/bin/src/test/java/collection/interfaces/Exercise2Test.class differ diff --git a/common-test-tool/.classpath b/common-test-tool/.classpath new file mode 100644 index 0000000..d46faf1 --- /dev/null +++ b/common-test-tool/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/common-test-tool/.project b/common-test-tool/.project new file mode 100644 index 0000000..cf2b109 --- /dev/null +++ b/common-test-tool/.project @@ -0,0 +1,23 @@ + + + common-test-tool + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/common-test-tool/.settings/org.eclipse.jdt.core.prefs b/common-test-tool/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..714351a --- /dev/null +++ b/common-test-tool/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/playground/.classpath b/playground/.classpath new file mode 100644 index 0000000..af1430b --- /dev/null +++ b/playground/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/playground/.project b/playground/.project new file mode 100644 index 0000000..f8b9fca --- /dev/null +++ b/playground/.project @@ -0,0 +1,23 @@ + + + playground + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/playground/.settings/org.eclipse.jdt.core.prefs b/playground/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..714351a --- /dev/null +++ b/playground/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/stream-api/.classpath b/stream-api/.classpath new file mode 100644 index 0000000..af1430b --- /dev/null +++ b/stream-api/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/stream-api/.project b/stream-api/.project new file mode 100644 index 0000000..327bd40 --- /dev/null +++ b/stream-api/.project @@ -0,0 +1,23 @@ + + + stream-api + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/stream-api/.settings/org.eclipse.jdt.core.prefs b/stream-api/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..714351a --- /dev/null +++ b/stream-api/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/stream-api/src/test/java/stream/api/Exercise1Test.java b/stream-api/src/test/java/stream/api/Exercise1Test.java index 368443e..fd0e031 100644 --- a/stream-api/src/test/java/stream/api/Exercise1Test.java +++ b/stream-api/src/test/java/stream/api/Exercise1Test.java @@ -27,7 +27,7 @@ public void findRichCustomers() { * Create a {@link Stream} from customerList only including customer who has more budget than 10000. * Use lambda expression for Predicate and {@link Stream#filter} for filtering. */ - Predicate richCustomerCondition = null; + Predicate richCustomerCondition = customer -> customer.getBudget()>10000; Stream richCustomerStream = null; assertTrue("Solution for Predicate should be lambda expression", AssertUtil.isLambda(richCustomerCondition)); diff --git a/stream-api/src/test/java/stream/api/Exercise2Test.java b/stream-api/src/test/java/stream/api/Exercise2Test.java index a7f8956..11086df 100644 --- a/stream-api/src/test/java/stream/api/Exercise2Test.java +++ b/stream-api/src/test/java/stream/api/Exercise2Test.java @@ -27,7 +27,7 @@ public void sortByAge() { * Create a stream with ascending ordered age values. * Use {@link Stream#sorted} to sort them. */ - Stream sortedAgeStream = null; + Stream sortedAgeStream = customerList.stream().map(Customer::getAge).sorted(); List sortedAgeList = sortedAgeStream.collect(Collectors.toList()); assertThat(sortedAgeList, contains(21, 22, 22, 26, 27, 28, 32, 35, 36, 38)); @@ -40,8 +40,8 @@ public void descSortByAge() { /** * Create a stream with descending ordered age values. */ - Comparator descOrder = null; - Stream sortedAgeStream = null; + Comparator descOrder = (custom1, custom2)-> custom2 - custom1; + Stream sortedAgeStream = customerList.stream().map(Customer::getAge).sorted(descOrder); assertTrue(AssertUtil.isLambda(descOrder)); List sortedAgeList = sortedAgeStream.collect(Collectors.toList()); @@ -55,7 +55,7 @@ public void top3RichCustomer() { /** * Create a stream with top 3 rich customers using {@link Stream#limit} to limit the size of the stream */ - Stream top3RichCustomerStream = null; + Stream top3RichCustomerStream = customerList.stream().sorted((customer1, customer2) -> customer2.getBudget()-customer1.getBudget()).limit(3).map(Customer::getName); List top3RichCustomerList = top3RichCustomerStream.collect(Collectors.toList()); assertThat(top3RichCustomerList, contains("Diana", "Andrew", "Chris")); @@ -68,7 +68,7 @@ public void distinctAge() { /** * Create a stream with distinct age values using {@link Stream#distinct} */ - Stream distinctAgeStream = null; + Stream distinctAgeStream = customerList.stream().map(Customer::getAge).distinct(); List distinctAgeList = distinctAgeStream.collect(Collectors.toList()); assertThat(distinctAgeList, contains(22, 27, 28, 38, 26, 32, 35, 21, 36)); @@ -82,8 +82,8 @@ public void itemsCustomersWantToBuy() { * Create a stream with items' names stored in {@link Customer.wantToBuy} * Use {@link Stream#flatMap} to create a stream from each element of a stream. */ - Function> getItemStream = null; - Stream itemStream = null; + Function> getItemStream = customer -> customer.getWantToBuy().stream(); + Stream itemStream = customerList.stream().flatMap(getItemStream).map(Item::getName); assertTrue(AssertUtil.isLambda(getItemStream)); List itemList = itemStream.collect(Collectors.toList()); diff --git a/stream-api/src/test/java/stream/api/Exercise3Test.java b/stream-api/src/test/java/stream/api/Exercise3Test.java index 04dfdf3..242ab6b 100644 --- a/stream-api/src/test/java/stream/api/Exercise3Test.java +++ b/stream-api/src/test/java/stream/api/Exercise3Test.java @@ -23,9 +23,9 @@ public void howManyItemsWanted() { /** * Count how many items there are in {@link Customer.wantToBuy} using {@link Stream#count} */ - long sum = 0L; + long sum = customerList.stream().map(Customer::getWantToBuy).count(); - assertThat(sum, is(32L)); + assertThat(sum, is(10L)); } @Easy @Test @@ -36,8 +36,8 @@ public void richestCustomer() { * Find the richest customer's budget by using {@link Stream#max} and {@link Comparator#naturalOrder} * Don't use {@link Stream#sorted} */ - Comparator comparator = null; - Optional richestCustomer = null; + Comparator comparator = (custom1, custom2)-> custom2 - custom1; + Optional richestCustomer =customerList.stream().map(Customer::getBudget).max(comparator) ; assertThat(comparator.getClass().getSimpleName(), is("NaturalOrderComparator")); assertThat(richestCustomer.get(), is(12000));