-
Notifications
You must be signed in to change notification settings - Fork 0
/
GettingProductPrices.java
executable file
·89 lines (59 loc) · 2.73 KB
/
GettingProductPrices.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
//DEPS org.javamoney.moneta:moneta-core:1.4.2
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.money.Monetary;
import javax.money.MonetaryAmount;
import org.javamoney.moneta.Money;
public class GettingProductPrices {
record Product(String description, MonetaryAmount price) { }
public static void main(String... args) {
var currency = Monetary.getCurrency("BRL");
var products = List.of(
new Product("bean", Money.of(5.99, currency)),
new Product("rice", Money.of(12.49, currency)),
new Product("coffee", Money.of(18.99, currency)),
new Product("bread", Money.of(6.59, currency)),
new Product("chocolate", Money.of(6.80, currency)));
gettingProdutPricesByUsingImperativeWay(products);
gettingProdutPricesByUsingForEach(products);
gettingProdutPricesByUsingStreamsWithLambdaExpression(products);
gettingProdutPricesByUsingStreamsWithMethodReference(products);
}
private static void gettingProdutPricesByUsingImperativeWay(List<Product> products) {
System.out.println("getting the product prices by using an imperative way:");
List<MonetaryAmount> prices = new ArrayList<>();
for (Product product : products) {
prices.add(product.price());
}
System.out.println(prices);
System.out.println();
}
private static void gettingProdutPricesByUsingForEach(List<Product> products) {
System.out.println("getting the product prices by using Iterator.forEach method:");
List<MonetaryAmount> prices = new ArrayList<>();
products.forEach(product -> prices.add(product.price()));
System.out.println(prices);
System.out.println("");
}
private static void gettingProdutPricesByUsingStreamsWithLambdaExpression(List<Product> products) {
System.out.println("getting the product prices by using Streams API with lambda expression:");
List<MonetaryAmount> prices = products
.stream()
.map(product -> product.price)
.collect(Collectors.toList());
System.out.println(prices);
System.out.println("");
}
private static void gettingProdutPricesByUsingStreamsWithMethodReference(List<Product> products) {
System.out.println("getting the product prices by using Streams API with Method Reference:");
List<MonetaryAmount> prices = products
.stream()
.map(Product::price)
.collect(Collectors.toList());
System.out.println(prices);
System.out.println("");
}
}