示例#1
0
  @SuppressWarnings("unchecked")
  @Test
  public void mustBeAbleToUseGroupBy() throws Exception {
    final Iterable<String> input = Arrays.asList("Aaa", "Abb", "Bcc", "Cdd", "Cee");
    final Source<List<String>, NotUsed> source =
        Source.from(input)
            .groupBy(
                3,
                new Function<String, String>() {
                  public String apply(String elem) {
                    return elem.substring(0, 1);
                  }
                })
            .grouped(10)
            .mergeSubstreams();

    final CompletionStage<List<List<String>>> future =
        source.grouped(10).runWith(Sink.<List<List<String>>>head(), materializer);
    final Object[] result = future.toCompletableFuture().get(1, TimeUnit.SECONDS).toArray();
    Arrays.sort(
        result,
        (Comparator<Object>)
            (Object)
                new Comparator<List<String>>() {
                  @Override
                  public int compare(List<String> o1, List<String> o2) {
                    return o1.get(0).charAt(0) - o2.get(0).charAt(0);
                  }
                });

    assertArrayEquals(
        new Object[] {
          Arrays.asList("Aaa", "Abb"), Arrays.asList("Bcc"), Arrays.asList("Cdd", "Cee")
        },
        result);
  }