BAEL-1330 Primitive Type Streams in Java 8 (#3106)

* initial commit for first article evaluation

* fixed compilation error

* removed final keywords for compliance

* added unit tests and refactored as needed

* refactored application-context.xml to use self-closing tags

* renamed tests to use UnitTest suffix

* removed unnecessary annotation

* added code samples for primitive streams

* removed sample code and tests for evaluation article

* renamed unit test to proper unit test naming

* added a test to show boxed method

* added the assertion to the test fox boxed

* changed mapToInt test to use Arrays.asList instead of a POJO as per Grzegorz and Eugen
This commit is contained in:
Jose Bob Santos Jr 2017-12-15 09:50:51 +08:00 committed by Grzegorz Piwowarek
parent 171d28a9d1
commit ae109dfb7c
2 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.stream;
import java.util.Arrays;
import java.util.stream.IntStream;
class PrimitiveStreams {
int min(int[] integers) {
return Arrays.stream(integers).min().getAsInt();
}
int max(int... integers) {
return IntStream.of(integers).max().getAsInt();
}
int sum(int... integers) {
return IntStream.of(integers).sum();
}
double avg(int... integers) {
return IntStream.of(integers).average().getAsDouble();
}
}

View File

@ -0,0 +1,97 @@
package com.baeldung.stream;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class PrimitiveStreamsUnitTest {
private PrimitiveStreams streams = new PrimitiveStreams();
@Test
public void givenAnArrayOfIntegersWhenMinIsCalledThenCorrectMinIsReturned() {
int[] integers = new int[] {20, 98, 12, 7, 35};
int min = streams.min(integers); // returns 7
assertEquals(7, min);
}
@Test
public void givenAnArrayOfIntegersWhenMaxIsCalledThenCorrectMaxIsReturned() {
int max = streams.max(20, 98, 12, 7, 35);
assertEquals(98, max);
}
@Test
public void givenAnArrayOfIntegersWhenSumIsCalledThenCorrectSumIsReturned() {
int sum = streams.sum(20, 98, 12, 7, 35);
assertEquals(172, sum);
}
@Test
public void givenAnArrayOfIntegersWhenAvgIsCalledThenCorrectAvgIsReturned() {
double avg = streams.avg(20, 98, 12, 7, 35);
assertTrue(34.4 == avg);
}
@Test
public void givenARangeOfIntegersWhenIntStreamSumIsCalledThenCorrectSumIsReturned() {
int sum = IntStream.range(1, 10).sum();
assertEquals(45, sum);
}
@Test
public void givenARangeClosedOfIntegersWhenIntStreamSumIsCalledThenCorrectSumIsReturned() {
int sum = IntStream.rangeClosed(1, 10).sum();
assertEquals(55, sum);
}
@Test
public void givenARangeWhenForEachIsCalledThenTheIndicesWillBePrinted() {
IntStream.rangeClosed(1, 5).parallel().forEach(System.out::println);
}
@Test
public void givenAnArrayWhenSumIsCalledThenTheCorrectSumIsReturned() {
int sum = Arrays.asList(33,45).stream().mapToInt(a -> a).sum();
assertEquals(78, sum);
}
@Test
public void givenAnIntStreamThenGetTheEvenIntegers() {
List<Integer> evenInts = IntStream.rangeClosed(1, 10)
.filter(i -> i % 2 == 0)
.boxed()
.collect(Collectors.toList());
List<Integer> expected = IntStream.of(2, 4, 6, 8, 10).boxed().collect(Collectors.toList());
assertEquals(expected, evenInts);
}
class Person {
private int age;
Person(int age) {
this.age = age;
}
int getAge() {
return age;
}
}
}