Adding Optional 2 Stream and Set.of examples.

This commit is contained in:
anton-k11 2016-09-20 17:59:43 +03:00
parent 6b7b8be333
commit f1cfc706df
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.baeldung.java9;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class OptionalToStreamTest {
@Test
public void testOptionalToStream(){
Optional<String> op = Optional.ofNullable("String value");
Stream<String> strOptionalStream = op.stream();
Stream<String> filteredStream = strOptionalStream.filter(
(str) -> { return str != null && str.startsWith("String"); }
);
assertEquals(1, filteredStream.count());
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.java9;
import java.util.Set;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SetExamplesTest {
@Test
public void testUnmutableSet(){
Set<String> strKeySet = Set.of("key1", "key2", "key3");
try{
strKeySet.add("newKey");
}catch(UnsupportedOperationException uoe){
}
assertEquals(strKeySet.size(), 3);
}
@Test
public void testArrayToSet(){
Integer [] intArray = new Integer[]{1,2,3,4,5,6,7,8,9,0};
Set<Integer> intSet = Set.of(intArray);
assertEquals(intSet.size(), intArray.length);
}
}