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:
committed by
Grzegorz Piwowarek
parent
18e1a3067d
commit
e7301029c3
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.stream.sum;
|
||||
|
||||
public class ArithmeticUtils {
|
||||
|
||||
public static int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user