Example usage for java.util.stream Stream iterate

List of usage examples for java.util.stream Stream iterate

Introduction

In this page you can find the example usage for java.util.stream Stream iterate.

Prototype

public static <T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) 

Source Link

Document

Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed , producing a Stream consisting of seed , f(seed) , f(f(seed)) , etc.

Usage

From source file:Main.java

public static void main(String[] args) {
    Stream.iterate(1, n -> n + 1).limit(10).forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) {
    Stream.iterate(2L, n -> n + 1).filter(Main::isOdd).limit(5).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    Stream.iterate(2L, n -> n + 1).filter(Main::isOdd).skip(100).limit(5).forEach(System.out::println);
}

From source file:Main.java

public static void main(String... args) {
    long l = Stream.iterate(1L, i -> i + 1).limit(3).reduce(Long::sum).get();
    System.out.println(l);/*from   ww w  .  j  a  va2s  . com*/
}

From source file:Main.java

public static void main(String... args) {
    long l = Stream.iterate(1L, i -> i + 1).limit(300).parallel().reduce(Long::sum).get();
    System.out.println(l);//from www  .java2 s  .c  o m
}

From source file:Main.java

public static void main(String[] args) {
    Stream<Long> tenNaturalNumbers = Stream.iterate(1L, n -> n + 1).limit(10);

    tenNaturalNumbers.forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    // fibonnaci with iterate
    Stream.iterate(new int[] { 0, 1 }, t -> new int[] { t[1], t[0] + t[1] }).limit(10)
            .forEach(t -> System.out.println("(" + t[0] + ", " + t[1] + ")"));
}

From source file:kymr.github.io.controller.FluxExampleController.java

private Flux<String> getList(String name) {
    return Flux.fromStream(Stream.iterate(1, i -> i + 1).limit(10)).map(i -> name + i);
}

From source file:de.qaware.chronix.storage.solr.timeseries.metric.MetricTimeSeries.java

/**
 * A stream over the points//from w ww . j  a va  2s. c  om
 *
 * @return the points as points
 */
public Stream<Point> points() {
    if (timestamps.isEmpty()) {
        return Stream.empty();
    }
    return Stream.iterate(of(0), pair -> of(pair.getIndex() + 1)).limit(timestamps.size());
}

From source file:com.ikanow.aleph2.example.flume_harvester.services.FlumeHarvestTechnology.java

/** Worker for starting a flume job or test
 * @param new_bucket - the bucket to start/test
 * @param context - the harvest context/*from w w w .j  a  v a2 s  .  c  om*/
 * @param enabled - whether the job is enabled
 * @param test_mode - whether the job is being run as a test
 * @return
 */
public CompletableFuture<BasicMessageBean> onNewSource(DataBucketBean new_bucket, IHarvestContext context,
        boolean enabled, boolean test_mode) {
    //TODO (ALEPH-10): unit test for this 

    try {
        // Create an agent per config element:

        if (enabled) {
            final List<FlumeBucketConfigBean> agents = FlumeUtils.getAgents(new_bucket);

            @SuppressWarnings("unused")
            final int stopped = removeAgentConfigs(new_bucket, 1);

            final Tuple2<String, Boolean> delete_result = FlumeLaunchUtils
                    .killProcess(FlumeLaunchUtils.getPid(new_bucket));
            //(safe side, always kill - should fail harmlessly if px already dead....)

            final List<Tuple2<String, File>> agent_paths = StreamUtils
                    .zip(agents.stream(), Stream.iterate(1, i -> i + 1), (a, b) -> Tuples._2T(a, b))
                    .map(Lambdas.wrap_u(agent_index -> updateAgentConfig(agent_index._2(), new_bucket,
                            agent_index._1(), context, test_mode)))
                    .collect(Collectors.toList());

            final List<Tuple2<String, String>> err_pids = agent_paths.stream().map(
                    agent_path -> FlumeLaunchUtils.launchProcess(new_bucket, _globals, agent_path, context))
                    .collect(Collectors.toList());

            if (err_pids.isEmpty()) {
                return CompletableFuture
                        .completedFuture(ErrorUtils.buildErrorMessage(this.getClass().getSimpleName(),
                                "onNewSource", "Found no valid Flume configs " + delete_result._1()));
            } else {
                final Tuple2<String, String> err_pid = err_pids.get(0);
                if (null != err_pid._1()) {
                    return CompletableFuture
                            .completedFuture(ErrorUtils.buildErrorMessage(this.getClass().getSimpleName(),
                                    "onNewSource", "Bucket error: " + err_pid._1() + " " + delete_result._1()));
                } else {
                    return CompletableFuture.completedFuture(
                            ErrorUtils.buildSuccessMessage(this.getClass().getSimpleName(), "onNewSource",
                                    "Bucket launched: " + err_pid._2() + " " + delete_result._1()));

                }
            }
        } else {
            return CompletableFuture.completedFuture(ErrorUtils.buildSuccessMessage(
                    this.getClass().getSimpleName(), "onNewSource", "Created in suspended mode"));
        }
    } catch (Exception e) {
        _logger.error(ErrorUtils.getLongForm("onNewSource: Unknown error {0}", e));
        return FutureUtils.returnError(e);
    }
}