Skip to content

Commit ee52998

Browse files
fscorromaibin
authored andcommitted
Collection to Stream without nulls in core-java-collections (eugenp#4903)
1 parent 5ccecc6 commit ee52998

6 files changed

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Collection;
4+
import java.util.stream.Stream;
5+
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
6+
7+
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNull {
8+
9+
/**
10+
* This method shows how to make a null safe stream from a collection through the use of
11+
* emptyIfNull() method from Apache Commons CollectionUtils library
12+
*
13+
* @param collection The collection that is to be converted into a stream
14+
* @return The stream that has been created from the collection or an empty stream if the collection is null
15+
*/
16+
public Stream<String> collectionAsStream(Collection<String> collection) {
17+
return emptyIfNull(collection).stream();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Collection;
4+
import java.util.Optional;
5+
import java.util.stream.Stream;
6+
7+
public class NullSafeCollectionStreamsUsingJava8OptionalContainer {
8+
9+
/**
10+
* This method shows how to make a null safe stream from a collection through the use of
11+
* Java SE 8’s Optional Container
12+
*
13+
* @param collection The collection that is to be converted into a stream
14+
* @return The stream that has been created from the collection or an empty stream if the collection is null
15+
*/
16+
public Stream<String> collectionAsStream(Collection<String> collection) {
17+
return Optional.ofNullable(collection)
18+
.map(Collection::stream)
19+
.orElseGet(Stream::empty);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Collection;
4+
import java.util.stream.Stream;
5+
6+
public class NullSafeCollectionStreamsUsingNullDereferenceCheck {
7+
8+
/**
9+
* This method shows how to make a null safe stream from a collection through the use of a check
10+
* to prevent null dereferences
11+
*
12+
* @param collection The collection that is to be converted into a stream
13+
* @return The stream that has been created from the collection or an empty stream if the collection is null
14+
*/
15+
public Stream<String> collectionAsStream(Collection<String> collection) {
16+
return collection == null ? Stream.empty() : collection.stream();
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.stream.Stream;
7+
import org.junit.Test;
8+
import static org.junit.Assert.*;
9+
10+
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest {
11+
12+
private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance =
13+
new NullSafeCollectionStreamsUsingCommonsEmptyIfNull();
14+
@Test
15+
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
16+
Collection<String> collection = null;
17+
Stream<String> expResult = Stream.empty();
18+
Stream<String> result = instance.collectionAsStream(collection);
19+
assertStreamEquals(expResult, result);
20+
}
21+
22+
@Test
23+
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
24+
Collection<String> collection = Arrays.asList("a", "b", "c");
25+
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
26+
Stream<String> result = instance.collectionAsStream(collection);
27+
assertStreamEquals(expResult, result);
28+
}
29+
30+
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
31+
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
32+
while (iter1.hasNext() && iter2.hasNext())
33+
assertEquals(iter1.next(), iter2.next());
34+
assert !iter1.hasNext() && !iter2.hasNext();
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.stream.Stream;
7+
import org.junit.Test;
8+
import static org.junit.Assert.*;
9+
10+
public class NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest {
11+
12+
private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance =
13+
new NullSafeCollectionStreamsUsingJava8OptionalContainer();
14+
@Test
15+
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
16+
Collection<String> collection = null;
17+
Stream<String> expResult = Stream.empty();
18+
Stream<String> result = instance.collectionAsStream(collection);
19+
assertStreamEquals(expResult, result);
20+
}
21+
22+
@Test
23+
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
24+
Collection<String> collection = Arrays.asList("a", "b", "c");
25+
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
26+
Stream<String> result = instance.collectionAsStream(collection);
27+
assertStreamEquals(expResult, result);
28+
}
29+
30+
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
31+
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
32+
while (iter1.hasNext() && iter2.hasNext())
33+
assertEquals(iter1.next(), iter2.next());
34+
assert !iter1.hasNext() && !iter2.hasNext();
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.stream.Stream;
7+
import org.junit.Test;
8+
import static org.junit.Assert.*;
9+
10+
public class NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest {
11+
12+
private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance =
13+
new NullSafeCollectionStreamsUsingNullDereferenceCheck();
14+
15+
@Test
16+
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
17+
Collection<String> collection = null;
18+
Stream<String> expResult = Stream.empty();
19+
Stream<String> result = instance.collectionAsStream(collection);
20+
assertStreamEquals(expResult, result);
21+
}
22+
23+
@Test
24+
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
25+
Collection<String> collection = Arrays.asList("a", "b", "c");
26+
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
27+
Stream<String> result = instance.collectionAsStream(collection);
28+
assertStreamEquals(expResult, result);
29+
}
30+
31+
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
32+
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
33+
while (iter1.hasNext() && iter2.hasNext())
34+
assertEquals(iter1.next(), iter2.next());
35+
assert !iter1.hasNext() && !iter2.hasNext();
36+
}
37+
38+
}

0 commit comments

Comments
 (0)