Example usage for java.util.stream Stream collect

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

Introduction

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

Prototype

<R, A> R collect(Collector<? super T, A, R> collector);

Source Link

Document

Performs a mutable reduction operation on the elements of this stream using a Collector .

Usage

From source file:com.hengyi.japp.tools.PYUtil.java

public static List<String> getFirstSpell(String cs) {
    if (isBlank(cs)) {
        return null;
    }/* www.  j  a  v a2s.c  om*/
    List<String> result = null;
    List<Set<Character>> cs_fpys = cs.chars().mapToObj(i -> toHanyuPinyinStringArray((char) i))
            .map(a -> Arrays.stream(a).map(s -> s.charAt(0)).collect(Collectors.toSet()))
            .collect(Collectors.toList());
    for (Set<Character> fpys : cs_fpys) {
        if (result == null) {
            result = fpys.stream().map(String::valueOf).collect(Collectors.toList());
        } else {
            Stream<String> tmps = result.stream().flatMap(s -> fpys.stream().map(fpy -> s + fpy));
            result = tmps.collect(Collectors.toList());
        }
    }
    return result;
}

From source file:com.liferay.apio.architect.internal.body.MultipartToBodyConverter.java

private static <T> Map<String, List<T>> _flattenMap(Map<String, Map<Integer, T>> indexedValueLists) {

    Set<Entry<String, Map<Integer, T>>> entries = indexedValueLists.entrySet();

    Stream<Entry<String, Map<Integer, T>>> stream = entries.stream();

    return stream.collect(Collectors.toMap(Entry::getKey, v -> {
        Map<Integer, T> map = v.getValue();

        return new ArrayList<>(map.values());
    }));//from w  ww  . java  2 s  . com
}

From source file:dyco4j.instrumentation.internals.CLI.java

private static Set<Path> getFilenames(final Path folder) throws IOException {
    final Stream<Path> _tmp1 = Files.walk(folder).filter(p -> p.toString().endsWith(".class"));
    return _tmp1.collect(Collectors.toSet());
}

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

/**
 * creates a literal structure from a stream of string elements,
 * the string stream will be build in a tree structure
 *
 * @param p_functor stream with functor strings
 * @param p_values value stream/*from w  w  w. ja v  a2  s .com*/
 * @return term
 */
protected static ITerm baseliteral(final Stream<String> p_functor, final Stream<ITerm> p_values) {
    final Stack<String> l_tree = p_functor.collect(Collectors.toCollection(Stack::new));

    ILiteral l_literal = CLiteral.from(l_tree.pop(), p_values);
    while (!l_tree.isEmpty())
        l_literal = CLiteral.from(l_tree.pop(), l_literal);

    return l_literal;
}

From source file:controllers.Facets.java

private static Promise<Result> createJsonResponse(
        Promise<org.elasticsearch.search.facet.Facets> facetsPromise) {
    Function<Entry<String, Facet>, JsonNode> toJson = mapEntry -> {
        TermsFacet facet = (TermsFacet) mapEntry.getValue();
        return Json.toJson(ImmutableMap.of(/*@formatter:off*/
                "field", facet.getName(), "count", facet.getTotalCount(), "entries",
                facet.getEntries().stream().map(facetEntry -> ImmutableMap.of("term",
                        facetEntry.getTerm().toString(), "count", facetEntry.getCount()))
                        .collect(Collectors.toList())));/*@formatter:on*/
    };/*from  w  ww.j  ava2s.c  o m*/
    return facetsPromise.map(facets -> {
        Stream<JsonNode> maps = facets.facetsAsMap().entrySet().stream().map(toJson);
        return ok(Json.toJson(maps.collect(Collectors.toList())));
    });
}

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

/**
 * creates a literal structure from a stream of string elements,
 * the string stream will be build in a tree structure
 *
 * @param p_functor stream with functor strings
 * @param p_values value stream/* w w  w.  j a va2s. c o m*/
 * @return term
 */
@Nonnull
protected static ITerm baseliteral(@Nonnull final Stream<String> p_functor,
        @Nonnull final Stream<ITerm> p_values) {
    final Stack<String> l_tree = p_functor.collect(Collectors.toCollection(Stack::new));

    ILiteral l_literal = CLiteral.from(l_tree.pop(), p_values);
    while (!l_tree.isEmpty())
        l_literal = CLiteral.from(l_tree.pop(), l_literal);

    return l_literal;
}

From source file:org.matsim.contrib.dvrp.util.chart.RouteCharts.java

private static Map<TaskStatus, CoordSource> createLinkSourceByStatus(Schedule schedule) {
    Stream<DriveTask> tasks = Schedules.driveTasks(schedule);

    // creating lists of DriveTasks
    Map<TaskStatus, List<DriveTask>> taskListByStatus = tasks
            .collect(Collectors.groupingBy(t -> t.getStatus()));

    // creating LinkSources
    Map<TaskStatus, CoordSource> linkSourceByStatus = new EnumMap<>(TaskStatus.class);
    for (TaskStatus ts : TaskStatus.values()) {
        linkSourceByStatus.put(ts, ScheduleCoordSources.createCoordSource(taskListByStatus.get(ts)));
    }/*from w  w  w.j av  a2s.  c  o  m*/

    return linkSourceByStatus;
}

From source file:msi.gama.util.GamaListFactory.java

/**
 * Create a GamaList from an array of objects, but does not attempt casting its values.
 *
 * @param contentType//  ww  w .j  a va 2s. c o m
 * @param collection
 * @warning ***WARNING*** This operation can end up putting values of the wrong type into the list
 * @return
 */

// public static <T> IList<T> create(final IType t, final Stream<T> stream)
// {
// return (IList<T>) createWithoutCasting(t, stream.toArray());
// }

public static <T> IList<T> create(final IType t, final Stream<T> stream) {
    return (IList<T>) stream.collect(TO_GAMA_LIST);
}

From source file:org.sonar.application.command.JvmOptionsTest.java

private static Map<String, String> shuffleThenToMap(Stream<Option> stream) {
    List<Option> options = stream.collect(Collectors.toList());
    Collections.shuffle(options);
    Map<String, String> res = new HashMap<>(options.size());
    for (Option option : options) {
        res.put(option.getPrefix(), option.getValue());
    }/*from w  ww  .  j  av  a  2 s .c o m*/
    return res;
}

From source file:com.simiacryptus.util.Util.java

/**
 * Gets last./*from  w  w w. jav a 2s  .c o m*/
 *
 * @param <T>    the type parameter
 * @param stream the stream
 * @return the last
 */
public static <T> T getLast(@javax.annotation.Nonnull final Stream<T> stream) {
    final List<T> collect = stream.collect(Collectors.toList());
    final T last = collect.get(collect.size() - 1);
    return last;
}