Skip to content

Commit e730102

Browse files
soufiane-cheouatipivovarit
authored andcommitted
Adding new methods for calculating the sum (eugenp#6088)
* Creating new module Creating new module for Hexagonal Architecture * Adding Java classes Adding Java classes used in the example * Adding the pom.xml file Adding the pom.xml file. * Update the pom.xml file Update the pom.xml file * BAEL-2498: Creating new folder Creating new folder for Java 8 sum with Streams examples * BAEL-2498: Uploading java Classes Adding the Java classes containing examples of different methods to calculate the sum using Java 8 Streams * BAEL-2498: Creating new folder Creating new folder for sum unit tests * BAEL-2498: Adding the unit tests Adding the unit tests for the methods that calculate the sum using the Java Streams * Add files via Upload - Adding a method for calculating the sum of a map´s values - Adding a method for calculating the sum of numbers within a String object * Add files via upload - Adding a method for calculating the sum of a map´s values - Adding a method for calculating the sum of numbers within a String object * Delete . ... * Delete pom.xml * Delete User.java * Delete UserControler.java * Delete UserDataBaseAdapter.java * Delete UserInterfaceAdapter.java * Delete UserDataBasePort.java * Delete UserInterfacePort.java * Delete UserService.java
1 parent 18e1a30 commit e730102

7 files changed

Lines changed: 274 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.stream.sum;
2+
3+
public class ArithmeticUtils {
4+
5+
public static int add(int a, int b) {
6+
return a + b;
7+
}
8+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.stream.sum;
2+
3+
public class Item {
4+
5+
private int id;
6+
private Integer price;
7+
8+
public Item(int id, Integer price) {
9+
super();
10+
this.id = id;
11+
this.price = price;
12+
}
13+
14+
// Standard getters and setters
15+
public long getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public Integer getPrice() {
24+
return price;
25+
}
26+
27+
public void setPrice(Integer price) {
28+
this.price = price;
29+
}
30+
31+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.stream.sum;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
public class StreamSumCalculator {
9+
10+
public static Integer getSumUsingCustomizedAccumulator(List<Integer> integers) {
11+
return integers.stream()
12+
.reduce(0, ArithmeticUtils::add);
13+
14+
}
15+
16+
public static Integer getSumUsingJavaAccumulator(List<Integer> integers) {
17+
return integers.stream()
18+
.reduce(0, Integer::sum);
19+
20+
}
21+
22+
public static Integer getSumUsingReduce(List<Integer> integers) {
23+
return integers.stream()
24+
.reduce(0, (a, b) -> a + b);
25+
26+
}
27+
28+
public static Integer getSumUsingCollect(List<Integer> integers) {
29+
30+
return integers.stream()
31+
.collect(Collectors.summingInt(Integer::intValue));
32+
33+
}
34+
35+
public static Integer getSumUsingSum(List<Integer> integers) {
36+
37+
return integers.stream()
38+
.mapToInt(Integer::intValue)
39+
.sum();
40+
}
41+
42+
public static Integer getSumOfMapValues(Map<Object, Integer> map) {
43+
44+
return map.values()
45+
.stream()
46+
.mapToInt(Integer::valueOf)
47+
.sum();
48+
}
49+
50+
public static Integer getSumIntegersFromString(String str) {
51+
52+
Integer sum = Arrays.stream(str.split(" "))
53+
.filter((s) -> s.matches("\\d+"))
54+
.mapToInt(Integer::valueOf)
55+
.sum();
56+
57+
return sum;
58+
}
59+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.stream.sum;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class StreamSumCalculatorWithObject {
7+
8+
public static Integer getSumUsingCustomizedAccumulator(List<Item> items) {
9+
return items.stream()
10+
.map(x -> x.getPrice())
11+
.reduce(0, ArithmeticUtils::add);
12+
}
13+
14+
public static Integer getSumUsingJavaAccumulator(List<Item> items) {
15+
return items.stream()
16+
.map(x -> x.getPrice())
17+
.reduce(0, Integer::sum);
18+
}
19+
20+
public static Integer getSumUsingReduce(List<Item> items) {
21+
return items.stream()
22+
.map(item -> item.getPrice())
23+
.reduce(0, (a, b) -> a + b);
24+
}
25+
26+
public static Integer getSumUsingCollect(List<Item> items) {
27+
return items.stream()
28+
.map(x -> x.getPrice())
29+
.collect(Collectors.summingInt(Integer::intValue));
30+
}
31+
32+
public static Integer getSumUsingSum(List<Item> items) {
33+
return items.stream()
34+
.mapToInt(x -> x.getPrice())
35+
.sum();
36+
}
37+
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.baeldung.stream.sum;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
public class StreamSumUnitTest {
13+
14+
@Test
15+
public void givenListOfIntegersWhenSummingUsingCustomizedAccumulatorThenCorrectValueReturned() {
16+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
17+
Integer sum = StreamSumCalculator.getSumUsingCustomizedAccumulator(integers);
18+
assertEquals(15, sum.intValue());
19+
20+
}
21+
22+
@Test
23+
public void givenListOfIntegersWhenSummingUsingJavaAccumulatorThenCorrectValueReturned() {
24+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
25+
Integer sum = StreamSumCalculator.getSumUsingJavaAccumulator(integers);
26+
assertEquals(15, sum.intValue());
27+
}
28+
29+
@Test
30+
public void givenListOfIntegersWhenSummingUsingReduceThenCorrectValueReturned() {
31+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
32+
Integer sum = StreamSumCalculator.getSumUsingReduce(integers);
33+
assertEquals(15, sum.intValue());
34+
}
35+
36+
@Test
37+
public void givenListOfIntegersWhenSummingUsingCollectThenCorrectValueReturned() {
38+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
39+
Integer sum = StreamSumCalculator.getSumUsingCollect(integers);
40+
assertEquals(15, sum.intValue());
41+
}
42+
43+
@Test
44+
public void givenListOfIntegersWhenSummingUsingSumThenCorrectValueReturned() {
45+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
46+
Integer sum = StreamSumCalculator.getSumUsingSum(integers);
47+
assertEquals(15, sum.intValue());
48+
}
49+
50+
@Test
51+
public void givenListOfItemsWhenSummingUsingCustomizedAccumulatorThenCorrectValueReturned() {
52+
Item item1 = new Item(1, 10);
53+
Item item2 = new Item(2, 15);
54+
Item item3 = new Item(3, 25);
55+
Item item4 = new Item(4, 40);
56+
57+
List<Item> items = Arrays.asList(item1, item2, item3, item4);
58+
59+
Integer sum = StreamSumCalculatorWithObject.getSumUsingCustomizedAccumulator(items);
60+
assertEquals(90, sum.intValue());
61+
62+
}
63+
64+
@Test
65+
public void givenListOfItemsWhenSummingUsingJavaAccumulatorThenCorrectValueReturned() {
66+
Item item1 = new Item(1, 10);
67+
Item item2 = new Item(2, 15);
68+
Item item3 = new Item(3, 25);
69+
Item item4 = new Item(4, 40);
70+
71+
List<Item> items = Arrays.asList(item1, item2, item3, item4);
72+
73+
Integer sum = StreamSumCalculatorWithObject.getSumUsingJavaAccumulator(items);
74+
assertEquals(90, sum.intValue());
75+
}
76+
77+
@Test
78+
public void givenListOfItemsWhenSummingUsingReduceThenCorrectValueReturned() {
79+
Item item1 = new Item(1, 10);
80+
Item item2 = new Item(2, 15);
81+
Item item3 = new Item(3, 25);
82+
Item item4 = new Item(4, 40);
83+
84+
List<Item> items = Arrays.asList(item1, item2, item3, item4);
85+
86+
Integer sum = StreamSumCalculatorWithObject.getSumUsingReduce(items);
87+
assertEquals(90, sum.intValue());
88+
}
89+
90+
@Test
91+
public void givenListOfItemsWhenSummingUsingCollectThenCorrectValueReturned() {
92+
Item item1 = new Item(1, 10);
93+
Item item2 = new Item(2, 15);
94+
Item item3 = new Item(3, 25);
95+
Item item4 = new Item(4, 40);
96+
97+
List<Item> items = Arrays.asList(item1, item2, item3, item4);
98+
99+
Integer sum = StreamSumCalculatorWithObject.getSumUsingCollect(items);
100+
assertEquals(90, sum.intValue());
101+
}
102+
103+
@Test
104+
public void givenListOfItemsWhenSummingUsingSumThenCorrectValueReturned() {
105+
Item item1 = new Item(1, 10);
106+
Item item2 = new Item(2, 15);
107+
Item item3 = new Item(3, 25);
108+
Item item4 = new Item(4, 40);
109+
110+
List<Item> items = Arrays.asList(item1, item2, item3, item4);
111+
112+
Integer sum = StreamSumCalculatorWithObject.getSumUsingSum(items);
113+
assertEquals(90, sum.intValue());
114+
}
115+
116+
@Test
117+
public void givenMapWhenSummingThenCorrectValueReturned() {
118+
Map<Object, Integer> map = new HashMap<Object, Integer>();
119+
map.put(1, 10);
120+
map.put(2, 15);
121+
map.put(3, 25);
122+
map.put(4, 40);
123+
124+
Integer sum = StreamSumCalculator.getSumOfMapValues(map);
125+
assertEquals(90, sum.intValue());
126+
}
127+
128+
@Test
129+
public void givenStringWhenSummingThenCorrectValueReturned() {
130+
String string = "Item1 10 Item2 25 Item3 30 Item4 45";
131+
132+
Integer sum = StreamSumCalculator.getSumIntegersFromString(string);
133+
assertEquals(110, sum.intValue());
134+
}
135+
136+
}

0 commit comments

Comments
 (0)