Just an example
@Test
public void testArraysToStream() {
String[] s1 = null;
String[] s2 = null;
Set<String> res = Stream.of(s1, s2)
.filter(Objects::nonNull) // <-- Filter out null values
.flatMap(Stream::of)
.collect(Collectors.toSet());
Assert.assertNotNull(res);
Assert.assertEquals(0,res.size());
s1 = new String[]{"1","2"};
Set<String> res2 = Stream.of(s1, s2)
.filter(Objects::nonNull) // <-- Filter out null values
.flatMap(Stream::of) // two arrays to one stream
.collect(Collectors.toSet());
Assert.assertNotNull(res2);
Assert.assertEquals(2,res2.size());
s2 = new String[]{"3","4"};
Set<String> res3 = Stream.of(s1, s2)
.filter(Objects::nonNull) // <-- Filter out null values
.flatMap(Stream::of) // two arrays to one stream
.collect(Collectors.toSet());
Assert.assertNotNull(res3);
Assert.assertEquals(4,res3.size());
Assert.assertNotNull(res3.iterator().next());
}
Комментариев нет:
Отправить комментарий