2017-01-11 22:10:56 +00:00
|
|
|
package com.baeldung.java8;
|
|
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
2017-01-15 15:15:11 +01:00
|
|
|
import static org.hamcrest.Matchers.anyOf;
|
|
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
2017-01-14 23:24:11 +00:00
|
|
|
|
2017-05-15 18:35:14 +02:00
|
|
|
public class Java8FindAnyFindFirstUnitTest {
|
2017-01-11 22:10:56 +00:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void createStream_whenFindAnyResultIsPresent_thenCorrect() {
|
|
|
|
|
|
2017-01-21 08:06:30 +01:00
|
|
|
List<String> list = Arrays.asList("A", "B", "C", "D");
|
2017-01-11 22:10:56 +00:00
|
|
|
|
|
|
|
|
Optional<String> result = list.stream().findAny();
|
|
|
|
|
|
2017-01-19 09:43:34 +00:00
|
|
|
assertTrue(result.isPresent());
|
2017-01-14 23:24:11 +00:00
|
|
|
assertThat(result.get(), anyOf(is("A"), is("B"), is("C"), is("D")));
|
2017-01-11 22:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
2017-01-19 09:43:34 +00:00
|
|
|
@Test
|
2017-01-21 08:06:30 +01:00
|
|
|
public void createParallelStream_whenFindAnyResultIsPresent_thenCorrect() throws Exception {
|
|
|
|
|
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
|
2017-01-29 16:03:33 +02:00
|
|
|
Optional<Integer> result = list.stream().parallel().filter(num -> num < 4).findAny();
|
2017-01-19 09:43:34 +00:00
|
|
|
|
|
|
|
|
assertTrue(result.isPresent());
|
2017-01-21 08:06:30 +01:00
|
|
|
assertThat(result.get(), anyOf(is(1), is(2), is(3)));
|
2017-01-19 09:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
2017-01-11 22:10:56 +00:00
|
|
|
@Test
|
|
|
|
|
public void createStream_whenFindFirstResultIsPresent_thenCorrect() {
|
|
|
|
|
|
2017-01-21 08:06:30 +01:00
|
|
|
List<String> list = Arrays.asList("A", "B", "C", "D");
|
2017-01-11 22:10:56 +00:00
|
|
|
|
|
|
|
|
Optional<String> result = list.stream().findFirst();
|
|
|
|
|
|
2017-01-19 09:43:34 +00:00
|
|
|
assertTrue(result.isPresent());
|
2017-01-21 08:06:30 +01:00
|
|
|
assertThat(result.get(), is("A"));
|
2017-01-11 22:10:56 +00:00
|
|
|
}
|
|
|
|
|
}
|