Implement Java Collections & Streams + Refactoring of packages

master
Hammy 4 years ago
parent 87b0097009
commit d6e3084423

1
.gitignore vendored

@ -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.apt.core.prefs
Personal/Test Driven Development/Hello World/.settings/org.eclipse.jdt.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 Personal/Test Driven Development/Hello World/.settings/org.eclipse.m2e.core.prefs
/.idea/

@ -8,7 +8,7 @@ public class ConditionalStatements {
System.out.print("Please enter your score: "); System.out.print("Please enter your score: ");
int score = in.nextInt(); int score = in.nextInt();
in.close(); in.close();
String output = "Your Grade is: "; String output = "Your Grade is: ";
// Checks if Grade is A // Checks if Grade is A
if (score >= 70) { if (score >= 70) {

@ -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<StreamProduct> 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);
}
}

@ -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);
}
}

@ -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<StreamProduct> 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<StreamProduct> streamProducts) {
}
private static void namesOfLightProductsWeightSortedLoop(
List<StreamProduct> streamProducts) {
List<StreamProduct> 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());
}
}
}

@ -1,4 +1,4 @@
package hello.world; package main.java.hello.world;
public class Greeting { public class Greeting {
String message; String message;

@ -1,4 +1,4 @@
package hello.world; package main.java.hello.world;
/** /**
* Hello world! * Hello world!

@ -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 org.junit.Test;
import static org.junit.Assert.assertEquals;
/** /**
* Unit test for simple App. * Unit test for simple App.
*/ */

Loading…
Cancel
Save