Implement Java Collections & Streams + Refactoring of packages
parent
87b0097009
commit
d6e3084423
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue