From d6e308442343029cb1e0a2f8a76d48f4134f4388 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sat, 7 Nov 2020 19:29:04 +0000 Subject: [PATCH] Implement Java Collections & Streams + Refactoring of packages --- .gitignore | 1 + .../src/ConditionalStatements.java | 2 +- Java Streams/src/EnterTheCollector.java | 26 ++++++++++ Java Streams/src/StreamProduct.java | 47 ++++++++++++++++++ Java Streams/src/StreamProducts.java | 43 ++++++++++++++++ .../src/main/java/hello/world/Greeting.java | 2 +- .../src/main/java/hello/world/HelloWorld.java | 2 +- .../test/java/hello/world/HelloWorldTest.java | 6 ++- .../target/classes/hello/world/Greeting.class | Bin 620 -> 783 bytes .../classes/hello/world/HelloWorld.class | Bin 701 -> 591 bytes .../hello/world/HelloWorldTest.class | Bin 875 -> 811 bytes 11 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 Java Streams/src/EnterTheCollector.java create mode 100644 Java Streams/src/StreamProduct.java create mode 100644 Java Streams/src/StreamProducts.java diff --git a/.gitignore b/.gitignore index e1aa7e6..7bd5c14 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ Personal/Test Driven Development/Hello World/.settings/org.eclipse.core.resource Personal/Test Driven Development/Hello World/.settings/org.eclipse.jdt.apt.core.prefs Personal/Test Driven Development/Hello World/.settings/org.eclipse.jdt.core.prefs Personal/Test Driven Development/Hello World/.settings/org.eclipse.m2e.core.prefs +/.idea/ diff --git a/2018 - 2019/2018/Conditionals/src/ConditionalStatements.java b/2018 - 2019/2018/Conditionals/src/ConditionalStatements.java index ba5b4fd..ed289b6 100644 --- a/2018 - 2019/2018/Conditionals/src/ConditionalStatements.java +++ b/2018 - 2019/2018/Conditionals/src/ConditionalStatements.java @@ -8,7 +8,7 @@ public class ConditionalStatements { System.out.print("Please enter your score: "); int score = in.nextInt(); in.close(); - + String output = "Your Grade is: "; // Checks if Grade is A if (score >= 70) { diff --git a/Java Streams/src/EnterTheCollector.java b/Java Streams/src/EnterTheCollector.java new file mode 100644 index 0000000..1bd1cf7 --- /dev/null +++ b/Java Streams/src/EnterTheCollector.java @@ -0,0 +1,26 @@ +import java.util.ArrayList; +import java.util.List; + +import static java.util.Comparator.comparingInt; + +public class EnterTheCollector { + public static void main(String[] args) { + StreamProduct door = new StreamProduct(1, "Wooden Door", 35); + StreamProduct floorPanel = new StreamProduct(2, "Floor Panel", 25); + StreamProduct window = new StreamProduct(3, "Glass Window", 10); + + List streamProducts = new ArrayList<>(); + streamProducts.add(door); + streamProducts.add(floorPanel); + streamProducts.add(window); + streamProducts.add(floorPanel); + streamProducts.add(window); + streamProducts.add(floorPanel); + + streamProducts + .stream() + .filter(product -> product.getWeight() < 30) + .sorted(comparingInt(StreamProduct::getWeight)) + .forEach(System.out::println); + } +} diff --git a/Java Streams/src/StreamProduct.java b/Java Streams/src/StreamProduct.java new file mode 100644 index 0000000..58785ad --- /dev/null +++ b/Java Streams/src/StreamProduct.java @@ -0,0 +1,47 @@ +import java.util.Objects; + +public class StreamProduct { + private final int id; + private final String name; + private final int weight; + + public StreamProduct(final int id, final String name, final int weight) { + this.id = id; + this.name = name; + this.weight = weight; + } + + public String getName() { + return name; + } + + public int getWeight() { + return weight; + } + + public int getId() { + return id; + } + + public String toString() { + return "Product{" + + "id=" + id + + ", name='" + name + '\'' + + ", weight=" + weight + + '}'; + } + + public boolean equals(final Object o) { + if (!(o instanceof StreamProduct)) return false; + + final StreamProduct streamProduct = (StreamProduct) o; + + return Objects.equals(id, streamProduct.id) + && Objects.equals(weight, streamProduct.weight) + && Objects.equals(name, streamProduct.name); + } + + public int hashCode() { + return Objects.hash(id, name, weight); + } +} diff --git a/Java Streams/src/StreamProducts.java b/Java Streams/src/StreamProducts.java new file mode 100644 index 0000000..6bed3c7 --- /dev/null +++ b/Java Streams/src/StreamProducts.java @@ -0,0 +1,43 @@ +import java.util.ArrayList; +import java.util.List; + +import static java.util.Comparator.comparingInt; + +public class StreamProducts { + public static void main(String[] args) { + StreamProduct door = new StreamProduct(1, "Wooden Door", 35); + StreamProduct floorPanel = new StreamProduct(2, "Floor Panel", 25); + StreamProduct window = new StreamProduct(3, "Glass Window", 10); + + List streamProducts = new ArrayList<>(); + streamProducts.add(door); + streamProducts.add(floorPanel); + streamProducts.add(window); + streamProducts.add(floorPanel); + streamProducts.add(window); + + namesOfLightProductsWeightSortedLoop(streamProducts); + namesOfLightProductsWeightSortedLoopStreamed(streamProducts); + } + + private static void namesOfLightProductsWeightSortedLoopStreamed(List streamProducts) { + + } + + private static void namesOfLightProductsWeightSortedLoop( + List streamProducts) { + List lightStreamProducts = new ArrayList<>(); + + for (StreamProduct streamProduct : streamProducts) { + if (streamProduct.getWeight() < 30) { + lightStreamProducts.add(streamProduct); + } + } + + lightStreamProducts.sort(comparingInt(StreamProduct::getWeight)); + + for (StreamProduct streamProduct : lightStreamProducts) { + System.out.println(streamProduct.getName()); + } + } +} diff --git a/Personal/Test Driven Development/Hello World/src/main/java/hello/world/Greeting.java b/Personal/Test Driven Development/Hello World/src/main/java/hello/world/Greeting.java index 1a1efab..05e23a4 100644 --- a/Personal/Test Driven Development/Hello World/src/main/java/hello/world/Greeting.java +++ b/Personal/Test Driven Development/Hello World/src/main/java/hello/world/Greeting.java @@ -1,4 +1,4 @@ -package hello.world; +package main.java.hello.world; public class Greeting { String message; diff --git a/Personal/Test Driven Development/Hello World/src/main/java/hello/world/HelloWorld.java b/Personal/Test Driven Development/Hello World/src/main/java/hello/world/HelloWorld.java index 9f638fc..f9cd300 100644 --- a/Personal/Test Driven Development/Hello World/src/main/java/hello/world/HelloWorld.java +++ b/Personal/Test Driven Development/Hello World/src/main/java/hello/world/HelloWorld.java @@ -1,4 +1,4 @@ -package hello.world; +package main.java.hello.world; /** * Hello world! diff --git a/Personal/Test Driven Development/Hello World/src/test/java/hello/world/HelloWorldTest.java b/Personal/Test Driven Development/Hello World/src/test/java/hello/world/HelloWorldTest.java index 5014521..c72fe29 100644 --- a/Personal/Test Driven Development/Hello World/src/test/java/hello/world/HelloWorldTest.java +++ b/Personal/Test Driven Development/Hello World/src/test/java/hello/world/HelloWorldTest.java @@ -1,8 +1,10 @@ -package hello.world; +package test.java.hello.world; -import static org.junit.Assert.assertEquals; +import main.java.hello.world.Greeting; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * Unit test for simple App. */ diff --git a/Personal/Test Driven Development/Hello World/target/classes/hello/world/Greeting.class b/Personal/Test Driven Development/Hello World/target/classes/hello/world/Greeting.class index f9f4e133ed53fb3d16df4cc71d8e4aa8963dd02c..701f0891be8348a0f749ffda6191362f402a3110 100644 GIT binary patch literal 783 zcmb7>O-~y!5Qbl;X*O&F32A`Raxk32B@q&*w&H?-OMn9(QjgB=C^2}~k=J}&`Bjx# z3GVzT#B3l+B(|zJc;xZSynbfp@#)V!fGw;AC<(NNWQ^_JSZCtyfg|!d9Rw%~G%nP& z>Kc^}y2t$sio8IOP?o6y2{e1t=!th+^FyHWQ>WVR2z+R7o(q(BZA<|^3Dl>_d(K%` z#k|1XS?VaW=9*#|*<_@R^4g|y>61HE%$2up zBW2E&)4BipSoTAm2`u-D#>t}z2jmYY!)vuS-+orb7lHNv&V;418L9BZUb%?&bxt-v z+243NSO;%{S1tH_M1^gc?*Xc8=h)lkXdVmyBK*~?{l>xrdnGKgt&AhwEZ=J{S@?_= pd$~e`?L?u(Z|_xD!t#v5lwqZSVWE&=^&bpH3e7@=ugqP-w`ZYtrpN#Q literal 620 zcmZvY%}&BV6ot=7ZDlBbQqZkCz{JM5)UYwZ4eA2Jet=0CQre^~`dXT3OkDT?K9uoJ zTYe%edS~v;={et>pWm-<0GBwmaKPZqr0<9BOBnf6_b!q$QGsW{V5rT-vv7S8c<$qP zE++|twUBWvJjrl8*xU}2i2k`|FmF_#k{&~$-5D_$x8YQB6i~8Z;fSF+P=S0{F2*u? z5@SEL2`9oIiAd=_KQxk=iWypieP|TZZ10A5Mnw>njYF6W+>^=u3e{@HaX*fK8Rt3X&TnEYr@; zM5v%joi?aZ)dqc1YAx3W-SZFBH!hVlnv+Y3w07#$%G9}&PSRK!q5&uObkm-IyYHyK ml6eNDWgvikK+QE!F9$OJ!DJ&fD9+geG1nmG8l;sbp8Nr4rec!- diff --git a/Personal/Test Driven Development/Hello World/target/classes/hello/world/HelloWorld.class b/Personal/Test Driven Development/Hello World/target/classes/hello/world/HelloWorld.class index ac46d30c797e3ef408eecb902109fb17efbdb0fc..049906402372a17114a34f0d7b64fedef7b8ac85 100644 GIT binary patch delta 360 zcmah^y-EW?7@R$gxm!=8G4ZEc(|nf) z88CX5KOsD8^9RJQqyn@E=O!D5CF1dq3(IJeim-wXO`61ael>LeXb2|_0eKnL2m`Et E0k%3+UjP6A literal 701 zcmZuv+fLg+5Iq|LPK-;^+<+EZC=?u0)#`JqzEo=a;9gLwpsG)suriAr8#xY$&w_^_ zwNgKzA60SIp$ZX^rMd2$nKR?R|9<@e@CL6eYODA6oNa> zT+_@V|L=lpv^()A>8lUgoT4d` zGNm(lK>dhb5i2M$Vu}w5nWL`%M)^mja)X+Gjn&o|n`3NG!B(=)McDjmgfyUyNg?*I RPtRt|MU$`s2lR7j-2%UDo5TPB diff --git a/Personal/Test Driven Development/Hello World/target/test-classes/hello/world/HelloWorldTest.class b/Personal/Test Driven Development/Hello World/target/test-classes/hello/world/HelloWorldTest.class index e6f339839f05900ec8e849a11bb502d745df4c4b..9df508a8857e87438c1079e50d280792d85027aa 100644 GIT binary patch literal 811 zcmbVKO>Yx15Pc4zNtZxEN`Mp&ZcpWs6;e+PLPCg&!}1{#f;c+6)~>=sgM8^5*5N-$JfTm+UgUT z@zj=;(!wiic;)O;%d$S=(e}KMJd>#wj*2Rgz7x6R`<^sR0_CL9IB7KQ^UTVc8|!%~ zd|Gf%SoxtMW#_Ty5x@9VSy>lA%J5W}K(Lhjx3nli7gu|@jB5;|gXFA}nRm+M)6i)@ zQAR#rmrLp9g2Xcn6PpUX5Ke{oG}-orsu}Jk7ms$zFwUKno~WP6x~6_-gqqg zcK&<^%?_{!S~xu%kYzNdEwUYo4x1Q=zM%Vd7=1$T8)aMQlMOYXZ$P%!_z|vS*yMKT gw1pd|RU`WU&31DGTYn?l?f+yOZDbphnih6{0&Sba)&Kwi literal 875 zcma)4U2oGc6g|$mrDS1i)2>^`fR3-VAQACggft-r4`EDnD&o;?t<;qli^PH7#S23q z@dNl#i0d??MQFgw{qVg$_nd2g|MBG;fJf-La2R%{LMa`7(5ae)FKqnAhNmJkE-DPQ z8Gp}1#gjOEHJXXBVOV=C6KS3>taN)rhRRbt5gt}>#m6ev7&arBh~xZhB+^qpQi36f z^q8w5Po>Sv!-|>8jA1XjoI#(V9j8JVnZz$emho5&raD)X6Q0r4_Uk+`@=Oe6CJFa2 zNwncc>V#^wNT+c)%ZX`d#q?bS49(?P&r9SgnhfiO_D&(*Lme$EW}9KF8!a|4Fex$V z_lCBk?PC|$7`#}Rk{k`__Wtbma2+>Y+#ps9QioY4Qsbk8eFmTOQ1?b|hq(yveP&~5|! zpx38g1hUM&I$4*pkUS&5djA~Nj}$oY$l5X}S|{tz8^~X!jA4VS4mQiK8hHzJLck9B z=2z6;290xUMM2}iC+vL2-U4@{AWF!@AwFqsS OChh!V(g87d(ESN+tG%-T