Skip to content
Open

ex3 #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>java8-lambda-stream-insta</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
26 changes: 26 additions & 0 deletions collection-interfaces/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions collection-interfaces/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>collection-interfaces</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions collection-interfaces/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions collection-interfaces/bin/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>collection-interfaces</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
20 changes: 20 additions & 0 deletions collection-interfaces/bin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java8-lambda-stream-insta</artifactId>
<groupId>java8-lambda-stream-insta</groupId>
<version>2.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>collection-interfaces</artifactId>

<dependencies>
<dependency>
<groupId>java8-lambda-stream-insta</groupId>
<artifactId>common-test-tool</artifactId>
</dependency>
</dependencies>
</project>
Binary file not shown.
Binary file not shown.
31 changes: 31 additions & 0 deletions common-test-tool/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions common-test-tool/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>common-test-tool</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions common-test-tool/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions playground/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions playground/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>playground</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions playground/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions stream-api/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions stream-api/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>stream-api</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions stream-api/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion stream-api/src/test/java/stream/api/Exercise1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Customer> richCustomerCondition = null;
Predicate<Customer> richCustomerCondition = customer -> customer.getBudget()>10000;
Stream<Customer> richCustomerStream = null;

assertTrue("Solution for Predicate should be lambda expression", AssertUtil.isLambda(richCustomerCondition));
Expand Down
14 changes: 7 additions & 7 deletions stream-api/src/test/java/stream/api/Exercise2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void sortByAge() {
* Create a stream with ascending ordered age values.
* Use {@link Stream#sorted} to sort them.
*/
Stream<Integer> sortedAgeStream = null;
Stream<Integer> sortedAgeStream = customerList.stream().map(Customer::getAge).sorted();

List<Integer> sortedAgeList = sortedAgeStream.collect(Collectors.toList());
assertThat(sortedAgeList, contains(21, 22, 22, 26, 27, 28, 32, 35, 36, 38));
Expand All @@ -40,8 +40,8 @@ public void descSortByAge() {
/**
* Create a stream with descending ordered age values.
*/
Comparator<Integer> descOrder = null;
Stream<Integer> sortedAgeStream = null;
Comparator<Integer> descOrder = (custom1, custom2)-> custom2 - custom1;
Stream<Integer> sortedAgeStream = customerList.stream().map(Customer::getAge).sorted(descOrder);

assertTrue(AssertUtil.isLambda(descOrder));
List<Integer> sortedAgeList = sortedAgeStream.collect(Collectors.toList());
Expand All @@ -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<String> top3RichCustomerStream = null;
Stream<String> top3RichCustomerStream = customerList.stream().sorted((customer1, customer2) -> customer2.getBudget()-customer1.getBudget()).limit(3).map(Customer::getName);

List<String> top3RichCustomerList = top3RichCustomerStream.collect(Collectors.toList());
assertThat(top3RichCustomerList, contains("Diana", "Andrew", "Chris"));
Expand All @@ -68,7 +68,7 @@ public void distinctAge() {
/**
* Create a stream with distinct age values using {@link Stream#distinct}
*/
Stream<Integer> distinctAgeStream = null;
Stream<Integer> distinctAgeStream = customerList.stream().map(Customer::getAge).distinct();

List<Integer> distinctAgeList = distinctAgeStream.collect(Collectors.toList());
assertThat(distinctAgeList, contains(22, 27, 28, 38, 26, 32, 35, 21, 36));
Expand All @@ -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<Customer, Stream<Item>> getItemStream = null;
Stream<String> itemStream = null;
Function<Customer, Stream<Item>> getItemStream = customer -> customer.getWantToBuy().stream();
Stream<String> itemStream = customerList.stream().flatMap(getItemStream).map(Item::getName);

assertTrue(AssertUtil.isLambda(getItemStream));
List<String> itemList = itemStream.collect(Collectors.toList());
Expand Down
8 changes: 4 additions & 4 deletions stream-api/src/test/java/stream/api/Exercise3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Integer> comparator = null;
Optional<Integer> richestCustomer = null;
Comparator<Integer> comparator = (custom1, custom2)-> custom2 - custom1;
Optional<Integer> richestCustomer =customerList.stream().map(Customer::getBudget).max(comparator) ;

assertThat(comparator.getClass().getSimpleName(), is("NaturalOrderComparator"));
assertThat(richestCustomer.get(), is(12000));
Expand Down