BAEL-1576 Added implementation for finding sum in a boxed integer array (#3901)

This commit is contained in:
raghav-jha 2018-03-28 22:07:33 +05:30 committed by pauljervis
parent 757f634467
commit 1de8475a99
2 changed files with 13 additions and 0 deletions

View File

@ -16,6 +16,10 @@ public class SumAndAverageInArray {
return Arrays.stream(array).sum(); return Arrays.stream(array).sum();
} }
public static int findSumUsingStream(Integer[] array) {
return Arrays.stream(array).mapToInt(Integer::intValue).sum();
}
public static double findAverageWithoutUsingStream(int[] array) { public static double findAverageWithoutUsingStream(int[] array) {
int sum = findSumWithoutUsingStream(array); int sum = findSumWithoutUsingStream(array);
return (double) sum / array.length; return (double) sum / array.length;

View File

@ -21,6 +21,15 @@ public class SumAndAverageInArrayTest {
Assert.assertEquals(expectedSumOfArray, actualSumOfArray); Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
} }
@Test
public void givenAnBoxedIntegerArray_whenUsingStream_thenFindSum() {
Integer[] array = new Integer[]{1, 3, 4, 8, 19, 20};
int expectedSumOfArray = 55;
int actualSumOfArray = SumAndAverageInArray.findSumUsingStream(array);
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
}
@Test @Test
public void givenAnIntArray_whenNotUsingStream_thenFindAverage() { public void givenAnIntArray_whenNotUsingStream_thenFindAverage() {
int[] array = { 1, 3, 4, 8, 19, 20 }; int[] array = { 1, 3, 4, 8, 19, 20 };