Example usage for java.util Collection stream

List of usage examples for java.util Collection stream

Introduction

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

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential Stream with this collection as its source.

Usage

From source file:Main.java

public static <K, V> Map<K, Collection<V>> toMap(final Collection<? extends V> items,
        final Function<V, K> mapper) {
    Map<K, Collection<V>> buffer = new HashMap<K, Collection<V>>();
    // TODO this isn't probably thread-safe
    items.stream().forEach(item -> {
        K key = mapper.apply(item);//from w w  w . j  a va2s  . c om
        Collection<V> c = buffer.getOrDefault(key, new ArrayList<V>());
        c.add(item);
        buffer.put(key, c);
    });
    return buffer;
}

From source file:org.fede.util.Util.java

public static <T> String list(Collection<T> elements, String separator) {
    return elements.stream().map(e -> e.toString()).collect(Collectors.joining(separator));
}

From source file:org.lightjason.agentspeak.action.builtin.rest.IBaseRest.java

/**
 * transforms a collection into a term stream
 *
 * @param p_collection collection/*from w ww.  ja v  a  2s.c  om*/
 * @return term stream
 */
@Nonnull
private static Stream<ITerm> flatcollection(@Nonnull final Collection<?> p_collection) {
    return p_collection.stream().flatMap(IBaseRest::flatterm);
}

From source file:dk.dma.db.cassandra.CassandraConnection.java

private static boolean seedsContainPortNumbers(Collection<String> connectionPoints) {
    requireNonNull(connectionPoints);//  w w  w  .j av a2s. com
    return connectionPoints.stream().filter(cp -> StringUtils.countMatches(cp, ":") == 1).count() > 0;
}

From source file:ch.sdi.core.util.ClassUtil.java

/**
 * Lists all types in the given package (recursive) which are annotated by the given annotation and
 * are assignable from given class./*from w w w  .j a  v a2 s  . c  om*/
 * <p>
 * All types which match the criteria are returned, no further checks (interface, abstract, embedded,
 * etc. are performed.
 * <p>
 *
 * @param aClass
 *        the desired (base) class
 * @param aAnnotation
 *        the desired annotation type
 * @param aRoot
 *        the package name where to start the search. Must not be empty. And not start
 *        with 'org.springframework' (cannot parse springs library itself).
 * @return a list of found types
 */
@SuppressWarnings("unchecked")
public static <T> Collection<Class<T>> findCandidatesByAnnotation(Class<T> aClass,
        Class<? extends Annotation> aAnnotation, String aRoot) {
    Collection<Class<T>> result = new ArrayList<Class<T>>();

    Collection<? extends Class<?>> candidates = findCandidatesByAnnotation(aAnnotation, aRoot);

    candidates.stream().peek(c -> myLog.trace("inspecting candidate " + c.getName()))
            .filter(c -> aClass.isAssignableFrom(c))
            .peek(c -> myLog.debug("candidate " + c.getName() + " is of desired type"))
            .forEach(c -> result.add((Class<T>) c));

    return result;

}

From source file:com.cloudera.oryx.common.OryxTest.java

private static <T> Collection<T> minus(Collection<? extends T> a, Collection<T> b) {
    return a.stream().filter(t -> !b.contains(t)).collect(Collectors.toList());
}

From source file:com.github.horrorho.inflatabledonkey.cloud.clients.SnapshotClient.java

public static List<Snapshot> snapshots(HttpClient httpClient, CloudKitty kitty, ProtectionZone zone,
        Collection<SnapshotID> snapshotIDs) throws IOException {

    if (snapshotIDs.isEmpty()) {
        return new ArrayList<>();
    }//from w  w w.j  a  v a  2  s .  c  om

    List<String> snapshots = snapshotIDs.stream().map(SnapshotID::id).collect(Collectors.toList());

    List<CloudKit.RecordRetrieveResponse> responses = kitty.recordRetrieveRequest(httpClient, "mbksync",
            snapshots);
    logger.debug("-- manifests() - responses: {}", responses);

    return responses.stream().filter(CloudKit.RecordRetrieveResponse::hasRecord)
            .map(CloudKit.RecordRetrieveResponse::getRecord).map(r -> manifests(r, zone))
            .filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
}

From source file:com.github.horrorho.liquiddonkey.cloud.data.FileGroups.java

static ICloud.MBSFileAuthTokens fileIdToSignatureAuthTokens(Collection<ICloud.MBSFile> files,
        Collection<ICloud.MBSFileAuthToken> fileIdAuthTokens) {

    Map<ByteString, ByteString> fileIdToSignature = files.stream()
            .collect(Collectors.toMap(ICloud.MBSFile::getFileID, ICloud.MBSFile::getSignature));

    // Somewhat confusing proto definitions.
    // Each file is requested by file signature/ checksum not by it's FileID
    ICloud.MBSFileAuthTokens.Builder builder = ICloud.MBSFileAuthTokens.newBuilder();
    fileIdAuthTokens.stream().forEach(token -> builder.addTokens(ICloud.MBSFileAuthToken.newBuilder()
            .setFileID(fileIdToSignature.get(token.getFileID())).setAuthToken(token.getAuthToken()).build()));
    return builder.build();
}

From source file:io.neba.core.util.JsonUtil.java

/**
 * @param collection must not be <code>null</code>.
 *//*www . j a va2  s.c  om*/
public static String toJson(Collection<?> collection) {
    if (collection == null) {
        throw new IllegalArgumentException("Method parameter collection must not be null");
    }

    return collection.stream().map(JsonUtil::toJson).reduce((l, r) -> l + "," + r).map(s -> '[' + s + ']')
            .orElse("[]");
}

From source file:com.cloudera.oryx.common.OryxTest.java

private static <T> String abbreviatedToString(Collection<T> c) {
    return c.size() <= 16 ? c.toString() : c.stream().limit(16).collect(Collectors.toList()) + "...";
}