Example usage for java.util.stream Stream skip

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

Introduction

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

Prototype

Stream<T> skip(long n);

Source Link

Document

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.

Usage

From source file:com.ikanow.aleph2.analytics.services.DeduplicationService.java

/**Tidiness util
 * @param in/*w w w  .ja  va  2 s.  c o m*/
 * @return
 */
private static Stream<JsonNode> deleteOtherDuplicates(final Stream<JsonNode> in) {
    return in.skip(1).map(j -> j.get(AnnotationBean._ID)).filter(j -> null != j);
}

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 w w  w . j a v a 2s . c  om
 *
 * 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;
            }//from ww  w. ja  va2 s  . c  o  m
        });
        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.ingini.mongodb.jongo.example.util.ImportQuotes.java

public static void main(String[] args) throws IOException, URISyntaxException {
    Jongo jongo = new Jongo(new MongoClient("127.0.0.1", 27017).getDB("movie_db"));
    MongoCollection quoteCollection = jongo.getCollection("quotes");

    URI uri = ImportQuotes.class.getClassLoader().getResource("raw/quotes.list").toURI();
    Stream<String> lines = Files.lines(Paths.get(uri), forName("windows-1252"));

    List<List<String>> quotesList = new ArrayList<>();

    lines.skip(14).forEach(s -> {
        List<String> quotes = new ArrayList<>();
        if (quotesList.size() == 0) {
            quotesList.add(quotes);//from   w ww.j av a2  s .co  m
        }
        quotes = quotesList.get(quotesList.size() - 1);

        if (s.isEmpty() && quotes.get(quotes.size() - 1).isEmpty()) {
            quotesList.add(new ArrayList<>());
        }

        quotes.add(s);
    });

    System.out.println("Quotes lists: " + quotesList.size());

    List<FilmQuote> filmQuotes = new ArrayList<>();
    for (List<String> quotes : quotesList) {
        Set<ActorQuote> actorQuotes = new LinkedHashSet<>();
        Iterator<String> iterator = quotes.subList(1, quotes.size()).iterator();

        String actorName = null;
        String quote = null;
        while (iterator.hasNext()) {
            String s = iterator.next();
            if (s.contains(":")) {
                if (actorName != null && quote != null) {
                    actorQuotes.add(new ActorQuote(actorName, quote));
                }
                actorName = StringUtils.substringBefore(s, ":");
                quote = StringUtils.substringAfter(s, ":");
            } else if (s.isEmpty()) {
                actorQuotes.add(new ActorQuote(actorName, quote));
            } else {
                quote += StringUtils.substringAfter(s, " ");
            }

        }
        FilmQuote filmQuote = new FilmQuote("english", quotes.get(0), actorQuotes);
        filmQuotes.add(filmQuote);
        quoteCollection.insert(filmQuote);
    }

    System.out.println("Film Quotes lists: " + filmQuotes.size());

}