Adding new methods for calculating the sum (#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
This commit is contained in:
soufiane-cheouati 2019-01-15 16:37:38 +00:00 committed by Grzegorz Piwowarek
parent 18e1a3067d
commit e7301029c3
7 changed files with 274 additions and 0 deletions

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,8 @@
package com.baeldung.stream.sum;
public class ArithmeticUtils {
public static int add(int a, int b) {
return a + b;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.stream.sum;
public class Item {
private int id;
private Integer price;
public Item(int id, Integer price) {
super();
this.id = id;
this.price = price;
}
// Standard getters and setters
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.stream.sum;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class StreamSumCalculator {
public static Integer getSumUsingCustomizedAccumulator(List<Integer> integers) {
return integers.stream()
.reduce(0, ArithmeticUtils::add);
}
public static Integer getSumUsingJavaAccumulator(List<Integer> integers) {
return integers.stream()
.reduce(0, Integer::sum);
}
public static Integer getSumUsingReduce(List<Integer> integers) {
return integers.stream()
.reduce(0, (a, b) -> a + b);
}
public static Integer getSumUsingCollect(List<Integer> integers) {
return integers.stream()
.collect(Collectors.summingInt(Integer::intValue));
}
public static Integer getSumUsingSum(List<Integer> integers) {
return integers.stream()
.mapToInt(Integer::intValue)
.sum();
}
public static Integer getSumOfMapValues(Map<Object, Integer> map) {
return map.values()
.stream()
.mapToInt(Integer::valueOf)
.sum();
}
public static Integer getSumIntegersFromString(String str) {
Integer sum = Arrays.stream(str.split(" "))
.filter((s) -> s.matches("\\d+"))
.mapToInt(Integer::valueOf)
.sum();
return sum;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.stream.sum;
import java.util.List;
import java.util.stream.Collectors;
public class StreamSumCalculatorWithObject {
public static Integer getSumUsingCustomizedAccumulator(List<Item> items) {
return items.stream()
.map(x -> x.getPrice())
.reduce(0, ArithmeticUtils::add);
}
public static Integer getSumUsingJavaAccumulator(List<Item> items) {
return items.stream()
.map(x -> x.getPrice())
.reduce(0, Integer::sum);
}
public static Integer getSumUsingReduce(List<Item> items) {
return items.stream()
.map(item -> item.getPrice())
.reduce(0, (a, b) -> a + b);
}
public static Integer getSumUsingCollect(List<Item> items) {
return items.stream()
.map(x -> x.getPrice())
.collect(Collectors.summingInt(Integer::intValue));
}
public static Integer getSumUsingSum(List<Item> items) {
return items.stream()
.mapToInt(x -> x.getPrice())
.sum();
}
}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,136 @@
package com.baeldung.stream.sum;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
public class StreamSumUnitTest {
@Test
public void givenListOfIntegersWhenSummingUsingCustomizedAccumulatorThenCorrectValueReturned() {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
Integer sum = StreamSumCalculator.getSumUsingCustomizedAccumulator(integers);
assertEquals(15, sum.intValue());
}
@Test
public void givenListOfIntegersWhenSummingUsingJavaAccumulatorThenCorrectValueReturned() {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
Integer sum = StreamSumCalculator.getSumUsingJavaAccumulator(integers);
assertEquals(15, sum.intValue());
}
@Test
public void givenListOfIntegersWhenSummingUsingReduceThenCorrectValueReturned() {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
Integer sum = StreamSumCalculator.getSumUsingReduce(integers);
assertEquals(15, sum.intValue());
}
@Test
public void givenListOfIntegersWhenSummingUsingCollectThenCorrectValueReturned() {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
Integer sum = StreamSumCalculator.getSumUsingCollect(integers);
assertEquals(15, sum.intValue());
}
@Test
public void givenListOfIntegersWhenSummingUsingSumThenCorrectValueReturned() {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
Integer sum = StreamSumCalculator.getSumUsingSum(integers);
assertEquals(15, sum.intValue());
}
@Test
public void givenListOfItemsWhenSummingUsingCustomizedAccumulatorThenCorrectValueReturned() {
Item item1 = new Item(1, 10);
Item item2 = new Item(2, 15);
Item item3 = new Item(3, 25);
Item item4 = new Item(4, 40);
List<Item> items = Arrays.asList(item1, item2, item3, item4);
Integer sum = StreamSumCalculatorWithObject.getSumUsingCustomizedAccumulator(items);
assertEquals(90, sum.intValue());
}
@Test
public void givenListOfItemsWhenSummingUsingJavaAccumulatorThenCorrectValueReturned() {
Item item1 = new Item(1, 10);
Item item2 = new Item(2, 15);
Item item3 = new Item(3, 25);
Item item4 = new Item(4, 40);
List<Item> items = Arrays.asList(item1, item2, item3, item4);
Integer sum = StreamSumCalculatorWithObject.getSumUsingJavaAccumulator(items);
assertEquals(90, sum.intValue());
}
@Test
public void givenListOfItemsWhenSummingUsingReduceThenCorrectValueReturned() {
Item item1 = new Item(1, 10);
Item item2 = new Item(2, 15);
Item item3 = new Item(3, 25);
Item item4 = new Item(4, 40);
List<Item> items = Arrays.asList(item1, item2, item3, item4);
Integer sum = StreamSumCalculatorWithObject.getSumUsingReduce(items);
assertEquals(90, sum.intValue());
}
@Test
public void givenListOfItemsWhenSummingUsingCollectThenCorrectValueReturned() {
Item item1 = new Item(1, 10);
Item item2 = new Item(2, 15);
Item item3 = new Item(3, 25);
Item item4 = new Item(4, 40);
List<Item> items = Arrays.asList(item1, item2, item3, item4);
Integer sum = StreamSumCalculatorWithObject.getSumUsingCollect(items);
assertEquals(90, sum.intValue());
}
@Test
public void givenListOfItemsWhenSummingUsingSumThenCorrectValueReturned() {
Item item1 = new Item(1, 10);
Item item2 = new Item(2, 15);
Item item3 = new Item(3, 25);
Item item4 = new Item(4, 40);
List<Item> items = Arrays.asList(item1, item2, item3, item4);
Integer sum = StreamSumCalculatorWithObject.getSumUsingSum(items);
assertEquals(90, sum.intValue());
}
@Test
public void givenMapWhenSummingThenCorrectValueReturned() {
Map<Object, Integer> map = new HashMap<Object, Integer>();
map.put(1, 10);
map.put(2, 15);
map.put(3, 25);
map.put(4, 40);
Integer sum = StreamSumCalculator.getSumOfMapValues(map);
assertEquals(90, sum.intValue());
}
@Test
public void givenStringWhenSummingThenCorrectValueReturned() {
String string = "Item1 10 Item2 25 Item3 30 Item4 45";
Integer sum = StreamSumCalculator.getSumIntegersFromString(string);
assertEquals(110, sum.intValue());
}
}