Example usage for java.util.stream Stream limit

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

Introduction

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

Prototype

Stream<T> limit(long maxSize);

Source Link

Document

Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

Usage

From source file:Main.java

public static void main(String[] args) {
    Stream.Builder<String> b = Stream.builder();
    b.accept("a");
    Stream<String> s = b.build();
    s.limit(10).forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.empty();
    s.limit(10).forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.of("a");
    s.limit(10).forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) {
    DoubleStream i = DoubleStream.generate(new Random()::nextDouble);
    Stream<Double> o = i.boxed();

    o.limit(10).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.of("a", "b");
    s.limit(10).forEach(System.out::println);

}

From source file:io.github.carlomicieli.footballdb.starter.pages.PageTests.java

@Test
public void shouldReturnStreamForChildrenElementsWithGivenTag() {
    String html = "<table id=\"my-table\">" + "<tr><td>1</td><td>one</td></tr>"
            + "<tr><td>2</td><td>two</td></tr>" + "<tr><td>3</td><td>three</td></tr>" + "</table>";
    Page page = page(fromHtml(html));//w w  w . j a  va2s .c om

    Element table = page.elementWithId("my-table").orElseThrow(NoSuchElementException::new);
    Stream<Element> stream = Page.childrenWithTag(table, "tr");
    Stream<Element> columns = stream.limit(1).flatMap(r -> Page.childrenWithTag(r, "td"));
    assertThat(columns.count()).isEqualTo(2);
}

From source file:com.spotify.heroic.shell.task.DeleteKeys.java

private AsyncFuture<Void> askForOk(final ShellIO io, final Stream<BackendKey> keys) {
    io.out().println("Examples of keys that would have been deleted (use --ok to " + "perform):");

    keys.limit(100).forEach(k -> {
        io.out().println(k.toString());/*from w ww .java2s  .  c om*/
    });

    keys.close();
    return async.resolved();
}

From source file:ai.grakn.engine.tasks.storage.TaskStateZookeeperStore.java

/**
 * This implementation will fetch all of the tasks from zookeeper and then
 * filer them out./*from   ww  w.j  a v a  2s . co  m*/
 *
 * ZK stores tasks by ID, so at the moment there is no more efficient way to search
 * within the storage itself.
 */
@Override
public Set<TaskState> getTasks(TaskStatus taskStatus, String taskClassName, String createdBy,
        EngineID engineRunningOn, int limit, int offset) {
    try {
        Stream<TaskState> stream = zookeeper.connection().getChildren().forPath(ALL_TASKS).stream()
                .map(TaskId::of).map(this::getState);

        if (taskStatus != null) {
            stream = stream.filter(t -> t.status().equals(taskStatus));
        }

        if (taskClassName != null) {
            stream = stream.filter(t -> t.taskClass().getName().equals(taskClassName));
        }

        if (createdBy != null) {
            stream = stream.filter(t -> t.creator().equals(createdBy));
        }

        if (engineRunningOn != null) {
            stream = stream.filter(t -> t.engineID() != null && t.engineID().equals(engineRunningOn));
        }

        stream = stream.skip(offset);

        if (limit > 0) {
            stream = stream.limit(limit);
        }

        return stream.collect(toSet());
    } catch (Exception e) {
        throw GraknBackendException.stateStorageTaskRetrievalFailure(e);
    }
}

From source file:ai.grakn.engine.tasks.manager.redisqueue.RedisTaskStorage.java

@Override
public Set<TaskState> getTasks(@Nullable TaskStatus taskStatus, @Nullable String taskClassName,
        @Nullable String createdBy, @Nullable EngineID runningOnEngine, int limit, int offset) {
    try (Jedis jedis = redis.getResource()) {
        Stream<TaskState> stream = jedis.keys(PREFIX + "*").stream().map(key -> {
            String v = jedis.get(key);
            try {
                return (TaskState) deserialize(Base64.getDecoder().decode(v));
            } catch (IllegalArgumentException e) {
                LOG.error("Could not decode key:value {}:{}", key, v);
                throw e;
            }/* w w  w.  jav a 2 s.c  om*/
        });
        if (taskStatus != null) {
            stream = stream.filter(t -> t.status().equals(taskStatus));
        }
        if (taskClassName != null) {
            stream = stream.filter(t -> t.taskClass().getName().equals(taskClassName));
        }
        if (createdBy != null) {
            stream = stream.filter(t -> t.creator().equals(createdBy));
        }
        if (runningOnEngine != null) {
            stream = stream.filter(t -> t.engineID() != null && t.engineID().equals(runningOnEngine));
        }
        stream = stream.skip(offset);
        if (limit > 0) {
            stream = stream.limit(limit);
        }
        Set<TaskState> results = stream.collect(toSet());
        LOG.debug("getTasks returning {} results", results.size());
        return results;
    } catch (Exception e) {
        throw GraknBackendException.stateStorageTaskRetrievalFailure(e);
    }
}

From source file:org.talend.dataprep.dataset.service.analysis.synchronous.QualityAnalysis.java

/**
 * Compute the quality (count, valid, invalid and empty) of the given dataset.
 *
 * @param dataset the dataset metadata./*from  ww w  .  ja  v a 2  s .c  om*/
 * @param records the dataset records
 * @param limit indicates how many records will be read from stream. Use a number < 0 to perform a full scan of
 */
public void computeQuality(DataSetMetadata dataset, Stream<DataSetRow> records, long limit) {
    // Compute valid / invalid / empty count, need data types for analyzer first
    final List<ColumnMetadata> columns = dataset.getRowMetadata().getColumns();
    if (columns.isEmpty()) {
        LOGGER.debug("Skip analysis of {} (no column information).", dataset.getId());
        return;
    }
    try (Analyzer<Analyzers.Result> analyzer = analyzerService.qualityAnalysis(columns)) {
        if (limit > 0) { // Only limit number of rows if limit > 0 (use limit to speed up sync analysis.
            LOGGER.debug("Limit analysis to the first {}.", limit);
            records = records.limit(limit);
        } else {
            LOGGER.debug("Performing full analysis.");
        }
        records.map(row -> row.toArray(DataSetRow.SKIP_TDP_ID)).forEach(analyzer::analyze);
        // Determine content size
        final List<Analyzers.Result> result = analyzer.getResult();
        adapter.adapt(columns, result);
        // Remember the number of records
        if (!result.isEmpty()) {
            final long recordCount = result.get(0).get(ValueQualityStatistics.class).getCount();
            dataset.getContent().setNbRecords((int) recordCount);
        }
    } catch (Exception e) {
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}