Java Code Examples for java.util.stream.Stream#iterate()
The following examples show how to use
java.util.stream.Stream#iterate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestStreamAPI.java From code with Apache License 2.0 | 6 votes |
/** * 创建Stream */ @Test public void test1() { // 1.可以通过Collection 系列集合提供的stream()或parallelStream() List<String> list = new ArrayList<>(); Stream<String> stream1 = list.stream(); // 2.通过 Arrays 中的静态方法stream()获取数组流 Employee[] emps = new Employee[10]; Stream<Employee> stream2 = Arrays.stream(emps); // 3.通过Stream 类中的静态方法of() Stream<String> stream3 = Stream.of("a", "b", "c"); // 4.创建无限流 // 迭代 Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 2); stream4.limit(10).forEach(System.out::println); // 生成 Stream.generate(Math::random) .limit(5) .forEach(System.out::println); }
Example 2
Source File: MoreStreamsTest.java From mug with Apache License 2.0 | 5 votes |
@Test public void flattenWithInfiniteOuterStream() throws Exception { Stream<List<Integer>> infinite = Stream.iterate( ImmutableList.of(1), l -> l.stream().map(i -> i + 1).collect(toImmutableList())); Stream<Integer> flattened = MoreStreams.flatten(infinite.map(l -> l.stream())); assertThat(flattened.limit(5).collect(toList())) .containsExactly(1, 2, 3, 4, 5).inOrder(); }
Example 3
Source File: StreamUtilsUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenInfiniteStream_whenTakeUntil10_shouldReturnStreamUpto10() { Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1); Stream<Integer> finiteInts = StreamUtils.takeUntil(infiniteInts, i -> i > 10); assertThat(finiteInts.collect(Collectors.toList()), hasSize(11)); }
Example 4
Source File: StreamUtilsUnitTest.java From tutorials with MIT License | 5 votes |
@Test // givenInfiniteStream_whenTakeWhile10_shouldReturnStreamOfSize10 public void givenInfiniteStream_whenTakeWhile10_shouldReturnStream() { Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1); Stream<Integer> finiteInts = StreamUtils.takeWhile(infiniteInts, i -> i < 10); assertThat(finiteInts.collect(Collectors.toList()), hasSize(10)); }
Example 5
Source File: ElementsSupplier.java From at.info-knowledge-base with MIT License | 5 votes |
default <T> void initElements(final T targetPage) { final Stream<Class> pageChain = Stream.iterate(targetPage.getClass(), Class::getSuperclass); takeWhile(pageChain, pageObject -> !pageObject.equals(Object.class)) .flatMap(pageObject -> Stream.of(pageObject.getDeclaredFields())) .filter(field -> field.isAnnotationPresent(HTML.class)) .forEach(field -> initElement(targetPage, field, field.getAnnotation(HTML.class))); }
Example 6
Source File: EmployeeTest.java From tutorials with MIT License | 5 votes |
@Test public void whenIterateStream_thenGetInfiniteStream() { Stream<Integer> evenNumStream = Stream.iterate(2, i -> i * 2); List<Integer> collect = evenNumStream .limit(5) .collect(Collectors.toList()); assertEquals(collect, Arrays.asList(2, 4, 8, 16, 32)); }
Example 7
Source File: TakeWhileTest.java From protonpack with MIT License | 5 votes |
@Test public void take_while_inclusive_takes_items_while_condition_is_met_but_includes_first_condition_breaker() { Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1); Stream<Integer> finiteInts = StreamUtils.takeWhileInclusive(infiniteInts, i -> i < 10); assertThat(finiteInts.collect(Collectors.toList()), hasSize(11)); }
Example 8
Source File: TakeWhileTest.java From protonpack with MIT License | 5 votes |
@Test public void take_while_takes_items_while_condition_is_met() { Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1); Stream<Integer> finiteInts = StreamUtils.takeWhile(infiniteInts, i -> i < 10); assertThat(finiteInts.collect(Collectors.toList()), hasSize(10)); }
Example 9
Source File: RepeatingSpliteratorTest.java From streams-utils with Apache License 2.0 | 5 votes |
@Test public void should_not_build_a_repeating_stream_on_a_non_sized_stream() { // Given Stream<String> strings = Stream.iterate("+", s -> s); // Then assertThatIllegalArgumentException().isThrownBy(() -> StreamsUtils.repeat(strings, 3)); }
Example 10
Source File: EmployeeTest.java From tutorials with MIT License | 5 votes |
@Test public void whenLimitInfiniteStream_thenGetFiniteElements() { Stream<Integer> infiniteStream = Stream.iterate(2, i -> i * 2); List<Integer> collect = infiniteStream .skip(3) .limit(5) .collect(Collectors.toList()); assertEquals(collect, Arrays.asList(16, 32, 64, 128, 256)); }
Example 11
Source File: OrderedStreamUtilsTest.java From stream-utils with Apache License 2.0 | 5 votes |
/** * This test validates that we don't get stack overflow exceptions. */ @Test @Ignore public void testNoStackOverflow() { final Stream<Integer> infiniteStream = Stream.iterate(0, i -> i + 1); // This is an infinite stream containing all numbers. We're going // to group all of them together, and see if the stack explodes. final Stream<List<Integer>> groupedStream = OrderedStreamUtils.groupBy(infiniteStream.limit(100000), i -> 1); assertEquals(100000, groupedStream.findFirst().get().size()); }
Example 12
Source File: OrderedStreamUtilsTest.java From stream-utils with Apache License 2.0 | 5 votes |
/** * This test is proves that the streamy way works. Observe how it doesn't * time out like the above test does. */ @Test(timeout = 500) public void testGroupByStreamy() { final Stream<Integer> infiniteStream = Stream.iterate(0, i -> i + 1); // This is an infinite stream containing all numbers. We're going to // chunk it up into batches of 10, and see if we can get the JVM to // explode. final Stream<List<Integer>> groupedStream = OrderedStreamUtils.groupBy(infiniteStream.limit(1000000000), i -> i / 10); assertEquals(groupedStream.findFirst().get(), Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); }
Example 13
Source File: OrderedStreamUtilsTest.java From stream-utils with Apache License 2.0 | 5 votes |
/** * This test is ignored, because it proves that java 8 collectors * materialize whole sets. See the next test. */ @Test(timeout = 500) @Ignore public void testGroupByNonStreamyShitty() { final Stream<Integer> infiniteStream = Stream.iterate(0, i -> i + 1); // This is an infinite stream containing all numbers. We're going to // chunk it up into batches of 10, and see if we can get the JVM to // explode. final Map<Integer, List<Integer>> collect = infiniteStream .limit(1000000000) .collect(Collectors.groupingBy(i -> i / 10)); assertEquals(collect.get(0), Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); }
Example 14
Source File: ElmTreeUtil.java From elm-plugin with MIT License | 4 votes |
private static Stream<Optional<PsiElement>> getSiblingsStream(PsiElement element) { return Stream.iterate( Optional.ofNullable(element), prev -> prev.map(e -> Optional.ofNullable(e.getNextSibling())).orElse(Optional.empty()) ); }
Example 15
Source File: MoreStreamsTest.java From more-lambdas-java with Artistic License 2.0 | 4 votes |
@Test void testPartition() { Stream<Integer> stream = Stream.iterate(1, i -> i + 1); MoreStreams.partition(stream, 100).limit(10).forEach(System.out::println); }
Example 16
Source File: StreamApi.java From tutorials with MIT License | 4 votes |
public static Integer getInfiniteStreamLastElementUsingReduce() { Stream<Integer> stream = Stream.iterate(0, i -> i + 1); return stream.limit(20).reduce((first, second) -> second).orElse(null); }
Example 17
Source File: ProtonpackUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenTakeUntil_thenTakenUntil() { Stream<Integer> streamOfInt = Stream.iterate(1, i -> i + 1); List<Integer> result = StreamUtils.takeUntil(streamOfInt, i -> i > 50).collect(Collectors.toList()); assertThat(result).contains(10, 20, 30, 40); }
Example 18
Source File: InfiniteStreams.java From tutorials with MIT License | 4 votes |
private static void doWhileStreamWay() { Stream<Integer> integers = Stream.iterate(0, i -> i + 1); integers.limit(10).forEach(System.out::println); }
Example 19
Source File: ProtonpackUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenTakeWhile_thenTakenWhile() { Stream<Integer> streamOfInt = Stream.iterate(1, i -> i + 1); List<Integer> result = StreamUtils.takeWhile(streamOfInt, i -> i < 5).collect(Collectors.toList()); assertThat(result).contains(1, 2, 3, 4); }
Example 20
Source File: StreamUtilsExample.java From tutorials with MIT License | 4 votes |
public void takeWhileAndTakeUntilStream() { Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1); Stream<Integer> finiteIntsWhileLessThan10 = StreamUtils.takeWhile(infiniteInts, i -> i < 10); Stream<Integer> finiteIntsUntilGreaterThan10 = StreamUtils.takeUntil(infiniteInts, i -> i > 10); }