From cca7078a6a469b571cd614fef03bd6737bb258b0 Mon Sep 17 00:00:00 2001 From: islembagga Date: Mon, 27 Mar 2017 16:59:11 +0200 Subject: [PATCH] ex3 3 exo --- .project | 17 ++++++++++ collection-interfaces/.classpath | 26 +++++++++++++++ collection-interfaces/.project | 23 +++++++++++++ .../.settings/org.eclipse.jdt.core.prefs | 5 +++ collection-interfaces/bin/.project | 23 +++++++++++++ collection-interfaces/bin/pom.xml | 20 +++++++++++ .../collection/interfaces/Exercise1Test.class | Bin 0 -> 6562 bytes .../collection/interfaces/Exercise2Test.class | Bin 0 -> 6593 bytes common-test-tool/.classpath | 31 ++++++++++++++++++ common-test-tool/.project | 23 +++++++++++++ .../.settings/org.eclipse.jdt.core.prefs | 5 +++ playground/.classpath | 26 +++++++++++++++ playground/.project | 23 +++++++++++++ .../.settings/org.eclipse.jdt.core.prefs | 5 +++ stream-api/.classpath | 26 +++++++++++++++ stream-api/.project | 23 +++++++++++++ .../.settings/org.eclipse.jdt.core.prefs | 5 +++ .../test/java/stream/api/Exercise1Test.java | 2 +- .../test/java/stream/api/Exercise2Test.java | 14 ++++---- .../test/java/stream/api/Exercise3Test.java | 8 ++--- 20 files changed, 293 insertions(+), 12 deletions(-) create mode 100644 .project create mode 100644 collection-interfaces/.classpath create mode 100644 collection-interfaces/.project create mode 100644 collection-interfaces/.settings/org.eclipse.jdt.core.prefs create mode 100644 collection-interfaces/bin/.project create mode 100644 collection-interfaces/bin/pom.xml create mode 100644 collection-interfaces/bin/src/test/java/collection/interfaces/Exercise1Test.class create mode 100644 collection-interfaces/bin/src/test/java/collection/interfaces/Exercise2Test.class create mode 100644 common-test-tool/.classpath create mode 100644 common-test-tool/.project create mode 100644 common-test-tool/.settings/org.eclipse.jdt.core.prefs create mode 100644 playground/.classpath create mode 100644 playground/.project create mode 100644 playground/.settings/org.eclipse.jdt.core.prefs create mode 100644 stream-api/.classpath create mode 100644 stream-api/.project create mode 100644 stream-api/.settings/org.eclipse.jdt.core.prefs 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 0000000000000000000000000000000000000000..f8a2fc88645bcfaaa549c2270d941063fe8f42ac GIT binary patch literal 6562 zcmeHLZEqVz5S~ra*ggy?X$yt&vP}y(P}!?*ZT=xKV%=0ynMU zH7BgAJz|Hf#&wbi?xa>Vshluf#)Yjlo(jXI6*t=`Sb#MG=QHVqsa|cr^9cVSu>Pr3 z(tSo?wX*$yz*=3$q5vfV8%MGYW3(y43#9zZePx8z$)SiT*Xcke%xk3sqdSR6?I$!U zwflm)(LhiSQ1!~HV`Y>xrL?0RLH_`$(x#)*QmJBTa6HKfKXru>ilc_E(J4MZ8REB? zR(Zm#rCpf_8`N^(j8wguYl#Ia_5&C&{+RUy-8wRN^~l^?G}gjWKf#na-lq=jAf66z zR+Imt>GpA_bleYWUq_hbc@tC@7@Ixn)ym2iB{~GfD%#Ks| z5%G+YBho}3Vn_-(V)dbQIu#~IEM-Y3 z)~Gg~KK!C%$P6Nhqf$%ej_p*Ym^8yJW7ud4u)0=um_LJ4IdD>^=gw5PzK(HPmGLmQ z{S1dH7G3lok|IvoQ%gp%$0&YQo~!@4o8&lUre@5{Z37yk(miBf$(hTGrT>nJrwFTg z8TYJ0zSX6IdcC%ij8qjw@G!i!}%2QLx0RB8Flww9GrW2WBm<7v=`$-QZjW}gX)xX=aOLx%axxW7bttVw zJ-2+;nU!X!X1(R(3jbVh`MhnX2$u=@d5TJIHsQET1TkxyNU!Ixi4wT@MPJ`xhhk4R zn(Cn*nmv|^B3vQl>$y38;dp3)@<=2GBL)ffX^@o~*g6r)*&@6}$X9do&oT);t&k!R zS?s;s9*tMcRYJb}KYC@6GFJj+jyayg4u((lig2BfALr(jg`=|PBjLq;YV6c|SRi2w zI~h+4m)`+ySzPR5uNRr}SeGMfoMHynR2&;G*y};&ydyCqGQ2-3!22kFvg3u(b5~*) zO!eB>nYM5hu;GpUbbtchor85e7qb5PD&7G^-$C)=X7mk|f54{|*ub-w0X)v*xjdGF zmtiw|cLBeyz(u@|@OlZaui$YK@znt0d%ZLBzMGM5F=4 o-5}!Dl!(^?i0vTaJI{c~1Bk2eE_!wa-h&EW0Y1QE4WnWEH+%a)<^TWy literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..8d97c1dd078e2a933c0e2dc35c2a95240a1c29f5 GIT binary patch literal 6593 zcmeHMTW=Fb6h32;_<~E)lyJXHLTQ}>wzxczC=$>hBAireLZm*9*Asiv-I>+QY)FLo zU6l%g_xuW;`4NaSyN1|gg&lC*hwg)ScGute=5o%=x$ORjf8PMWJ*ed&N8q;6Ud?i2 zYujvx)jZ|<+_OT-nvj<3E#`4kTi)f`6NcZ}Kwuum2u#IjpUGD3kH$7i5I7OAkid9- zm4yogitC~!nT<3jFz4Pa)(=G1troJiP+!3=T3jzIp}@qVkisq@Pi20Sz}O?zs_NFN>V{NJXG;1NL8!EKLknlu^4E@=`G|-#(y@ZTCSv zVWvIO4&9~*xNR%k5|OPWQ@KfTE2J!<+XZgQRPyJvGt|Y7NN0XamH8Jh6Knq8(=Ic! z*GJ#(0PO=AsW}X0em2)j)kf8G2b5lRVrqBI_!|T&$r{(K&HJ}>L4)ppQM~RCa1}HPD|0HLE9*fbkoHJ_4n66={z@ z7I8G!WAt(PIS=y$@*OXqhZ|Tp$4Ea9w|a{6iMJt8UKPgR9@)TZo`G3FG=2oY%>&~w zfo&e2a!|l0EZsv1uP5V=1Sjwgp!5pLe@&nI2d3V}xS#Oqq#VO(yp|Ib%)puW-C1PF z!8yEl?Rji3;O{Ww#T3T#sf?EgWW18Xcs-So4#+s0!gx28@!EilKc_H0N@2{wFSu$s Q_!X+y7Eogq_ziA;1TfNP%>V!Z literal 0 HcmV?d00001 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));