Skip to content

Commit f1cfc70

Browse files
committed
Adding Optional 2 Stream and Set.of examples.
1 parent 6b7b8be commit f1cfc70

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.java9;
2+
3+
import java.util.Optional;
4+
import java.util.stream.Stream;
5+
6+
import org.junit.Test;
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class OptionalToStreamTest {
10+
11+
@Test
12+
public void testOptionalToStream(){
13+
Optional<String> op = Optional.ofNullable("String value");
14+
Stream<String> strOptionalStream = op.stream();
15+
Stream<String> filteredStream = strOptionalStream.filter(
16+
(str) -> { return str != null && str.startsWith("String"); }
17+
);
18+
assertEquals(1, filteredStream.count());
19+
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.java9;
2+
3+
4+
import java.util.Set;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class SetExamplesTest {
10+
11+
@Test
12+
public void testUnmutableSet(){
13+
Set<String> strKeySet = Set.of("key1", "key2", "key3");
14+
try{
15+
strKeySet.add("newKey");
16+
}catch(UnsupportedOperationException uoe){
17+
}
18+
assertEquals(strKeySet.size(), 3);
19+
}
20+
21+
@Test
22+
public void testArrayToSet(){
23+
Integer [] intArray = new Integer[]{1,2,3,4,5,6,7,8,9,0};
24+
Set<Integer> intSet = Set.of(intArray);
25+
assertEquals(intSet.size(), intArray.length);
26+
}
27+
}

0 commit comments

Comments
 (0)