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> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner);

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.of("a", "b", "c");

    List<String> names = s.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

    System.out.println(names);/* www .j ava 2s.c  o  m*/
}

From source file:org.jenetics.stat.MinMaxTest.java

@Test
public void parallelMinMax() {
    final Stream<Integer> stream = IntStream.range(0, 100).boxed().parallel();
    final MinMax<Integer> minMax = stream.collect(MinMax::of, MinMax::accept, MinMax::combine);

    Assert.assertEquals(minMax.getMax(), Integer.valueOf(99));
    Assert.assertEquals(minMax.getMin(), Integer.valueOf(0));
    Assert.assertEquals(100, minMax.getCount());
}

From source file:jp.co.opentone.bsol.linkbinder.service.correspon.impl.CorresponFullTextSearchServiceImpl.java

private void setSummaryData(SearchFullTextSearchCorresponCondition condition,
        ElasticsearchSearchResultRecord rec, FullTextSearchCorresponsResult r) {
    Stream<String> highlights = null;
    switch (condition.getFullTextSearchMode()) {
    case ALL://from   ww  w  . j  a  v  a  2 s. c om
        highlights = Stream
                .of(rec.getHighlightedFragments("body"), rec.getHighlightedFragments("attachments.name"),
                        rec.getHighlightedFragments("attachments.content.content"))
                .flatMap(Function.identity());
        break;
    case SUBJECT:
        highlights = rec.getHighlightedFragments("title");
        break;
    case SUBJECT_AND_BODY:
        highlights = rec.getHighlightedFragments("body");
        break;
    case ATTACHED_FILE:
        highlights = Stream
                .of(rec.getHighlightedFragments("attachments.name"),
                        rec.getHighlightedFragments("attachments.content.content"))
                .flatMap(Function.identity());
        break;
    default:
    }

    List<FullTextSearchSummaryData> summaryDataList = highlights.collect(
            () -> new ArrayList<FullTextSearchSummaryData>(),
            (t, v) -> t.add(new FullTextSearchSummaryData(v, false)), (t, u) -> t.addAll(u));

    if (condition.isIncludeImage()) {
        summaryDataList.addAll(rec.getHighlightedFragments("attachments.extractedText").collect(
                () -> new ArrayList<FullTextSearchSummaryData>(),
                (t, v) -> t.add(new FullTextSearchSummaryData(v, false)), (t, u) -> t.addAll(u)));
    }
    r.setSummaryList(summaryDataList);
}