Add Streams.of(Iterable<E>)
This commit is contained in:
parent
152b1777fd
commit
9be39c4a7a
|
@ -158,6 +158,7 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<action issue="LANG-1627" type="add" dev="ggregory" due-to="Alberto Scotto, Avijit Chakraborty, Steve Bosman, Bruno P. Kinoshita, Gary Gregory">Add ArrayUtils.oneHot().</action>
|
||||
<action issue="LANG-1662" type="add" dev="ggregory" due-to="Daniel Augusto Veronezi Salvador, Gary Gregory, Bruno P. Kinoshita">Let ReflectionToStringBuilder only reflect given field names #849.</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.of(Enumeration<E>).</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.of(Iterable<E>).</action>
|
||||
<!-- UPDATE -->
|
||||
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.5 #742, #752, #764, #833, #867.</action>
|
||||
<action type="update" dev="ggregory" due-to="Dependabot">Bump actions/checkout from 2 to 3 #819, #825, #859.</action>
|
||||
|
|
|
@ -641,6 +641,18 @@ public class Streams {
|
|||
return StreamSupport.stream(new EnumerationSpliterator<>(Long.MAX_VALUE, Spliterator.ORDERED, enumeration), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a stream on the given Iterable.
|
||||
*
|
||||
* @param <E> the type of elements in the Iterable.
|
||||
* @param iterable the Iterable to stream or null.
|
||||
* @return a new Stream or {@link Stream#empty()} if the Iterable is null.
|
||||
* @since 3.13.0
|
||||
*/
|
||||
public static <E> Stream<E> of(final Iterable<E> iterable) {
|
||||
return iterable == null ? Stream.empty() : StreamSupport.stream(iterable.spliterator(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Null-safe version of {@link Stream#of(Object[])}.
|
||||
*
|
||||
|
|
|
@ -188,6 +188,17 @@ public class StreamsTest extends AbstractLangTest {
|
|||
assertEquals(2, collect.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfIterableNotNull() {
|
||||
assertEquals(2, Streams.of((Iterable<String>) Arrays.asList("A", "B")).collect(Collectors.toList()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfIterableNull() {
|
||||
final Iterable<String> input = null;
|
||||
assertEquals(0, Streams.of(input).collect(Collectors.toList()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleStreamFilter() {
|
||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||
|
|
Loading…
Reference in New Issue