BAEL-2934 IntStreams conversions
(cherry picked from commit 11abb325ecb2f299c318d82952e7718bab03510c)
This commit is contained in:
parent
69f947e3cd
commit
44dfb1a076
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.intstreams.conversion;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class IntStreamsConversionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void intStreamToArray() {
|
||||
int[] first50EvenNumbers = IntStream.iterate(0, i -> i + 2)
|
||||
.limit(50)
|
||||
.toArray();
|
||||
|
||||
assertThat(first50EvenNumbers).hasSize(50);
|
||||
assertThat(first50EvenNumbers[2]).isEqualTo(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intStreamToList() {
|
||||
List<Integer> first50IntegerNumbers = IntStream.range(0, 50)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(first50IntegerNumbers).hasSize(50);
|
||||
assertThat(first50IntegerNumbers.get(2)).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intStreamToString() {
|
||||
String first3numbers = IntStream.range(0, 3)
|
||||
.mapToObj(String::valueOf)
|
||||
.collect(Collectors.joining(", ", "[", "]"));
|
||||
|
||||
assertThat(first3numbers).isEqualTo("[0, 1, 2]");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue