@Test
  public void testGroupingByWithDomain() {
    List<String> data = Arrays.asList("a", "foo", "test", "ququq", "bar", "blahblah");
    Collector<String, ?, String> collector =
        MoreCollectors.collectingAndThen(
            MoreCollectors.groupingBy(
                String::length,
                IntStreamEx.range(10).boxed().toSet(),
                TreeMap::new,
                MoreCollectors.first()),
            Object::toString);
    checkShortCircuitCollector(
        "groupingWithDomain",
        "{0=Optional.empty, 1=Optional[a], 2=Optional.empty, 3=Optional[foo], 4=Optional[test], 5=Optional[ququq], "
            + "6=Optional.empty, 7=Optional.empty, 8=Optional[blahblah], 9=Optional.empty}",
        data.size(),
        data::stream,
        collector);

    Map<String, String> name2sex = new LinkedHashMap<>();
    name2sex.put("Mary", "Girl");
    name2sex.put("John", "Boy");
    name2sex.put("James", "Boy");
    name2sex.put("Lucie", "Girl");
    name2sex.put("Fred", "Boy");
    name2sex.put("Thomas", "Boy");
    name2sex.put("Jane", "Girl");
    name2sex.put("Ruth", "Girl");
    name2sex.put("Melanie", "Girl");
    Collector<Entry<String, String>, ?, Map<String, List<String>>> groupingBy =
        MoreCollectors.groupingBy(
            Entry::getValue,
            StreamEx.of("Girl", "Boy").toSet(),
            MoreCollectors.mapping(Entry::getKey, MoreCollectors.head(2)));
    AtomicInteger counter = new AtomicInteger();
    Map<String, List<String>> map =
        EntryStream.of(name2sex).peek(c -> counter.incrementAndGet()).collect(groupingBy);
    assertEquals(Arrays.asList("Mary", "Lucie"), map.get("Girl"));
    assertEquals(Arrays.asList("John", "James"), map.get("Boy"));
    assertEquals(4, counter.get());

    Collector<Entry<String, String>, ?, Map<String, String>> groupingByJoin =
        MoreCollectors.groupingBy(
            Entry::getValue,
            StreamEx.of("Girl", "Boy").toSet(),
            MoreCollectors.mapping(
                Entry::getKey, Joining.with(", ").maxChars(16).cutAfterDelimiter()));
    counter.set(0);
    Map<String, String> mapJoin =
        EntryStream.of(name2sex).peek(c -> counter.incrementAndGet()).collect(groupingByJoin);
    assertEquals("Mary, Lucie, ...", mapJoin.get("Girl"));
    assertEquals("John, James, ...", mapJoin.get("Boy"));
    assertEquals(7, counter.get());
  }
  @Test
  public void testFirstLast() {
    Supplier<Stream<Integer>> s = () -> IntStreamEx.range(1000).boxed();
    checkShortCircuitCollector("first", Optional.of(0), 1, s, MoreCollectors.first());
    checkShortCircuitCollector(
        "firstLong",
        Optional.of(0),
        1,
        () -> Stream.of(1).flatMap(x -> IntStream.range(0, 1000000000).boxed()),
        MoreCollectors.first(),
        true);
    checkShortCircuitCollector(
        "first",
        Optional.of(1),
        1,
        () -> Stream.iterate(1, x -> x + 1),
        MoreCollectors.first(),
        true);
    assertEquals(
        1, (int) StreamEx.iterate(1, x -> x + 1).parallel().collect(MoreCollectors.first()).get());

    checkCollector("last", Optional.of(999), s, MoreCollectors.last());
    checkCollectorEmpty("first", Optional.empty(), MoreCollectors.first());
    checkCollectorEmpty("last", Optional.empty(), MoreCollectors.last());
  }
 @Test(expected = IllegalStateException.class)
 public void testGroupingByWithDomainException() {
   List<Integer> list = Arrays.asList(1, 2, 20, 3, 31, 4);
   Collector<Integer, ?, Map<Integer, List<Integer>>> c =
       MoreCollectors.groupingBy(
           i -> i % 10, StreamEx.of(0, 1, 2, 3).toSet(), Collectors.toList());
   Map<Integer, List<Integer>> map = list.stream().collect(c);
   System.out.println(map);
 }
  @Test
  public void testGreatest() {
    List<Integer> ints = IntStreamEx.of(new Random(1), 1000, 1, 1000).boxed().toList();
    List<Integer> sorted = StreamEx.of(ints).sorted().toList();
    List<Integer> revSorted = StreamEx.of(ints).reverseSorted().toList();
    Comparator<Integer> byString = Comparator.comparing(String::valueOf);
    checkShortCircuitCollector(
        "least(0)", Collections.emptyList(), 0, ints::stream, MoreCollectors.least(0));
    checkCollector("least(5)", sorted.subList(0, 5), ints::stream, MoreCollectors.least(5));
    checkCollector("least(20)", sorted.subList(0, 20), ints::stream, MoreCollectors.least(20));
    checkCollector("least(MAX)", sorted, ints::stream, MoreCollectors.least(Integer.MAX_VALUE));
    checkCollector(
        "least(byString, 20)",
        StreamEx.of(ints).sorted(byString).limit(20).toList(),
        ints::stream,
        MoreCollectors.least(byString, 20));

    checkShortCircuitCollector(
        "greatest(0)", Collections.emptyList(), 0, ints::stream, MoreCollectors.greatest(0));
    checkCollector(
        "greatest(5)", revSorted.subList(0, 5), ints::stream, MoreCollectors.greatest(5));
    checkCollector(
        "greatest(20)", revSorted.subList(0, 20), ints::stream, MoreCollectors.greatest(20));
    checkCollector(
        "greatest(MAX)", revSorted, ints::stream, MoreCollectors.greatest(Integer.MAX_VALUE));
    checkCollector(
        "greatest(byString, 20)",
        StreamEx.of(ints).reverseSorted(byString).limit(20).toList(),
        ints::stream,
        MoreCollectors.greatest(byString, 20));

    Supplier<Stream<Integer>> s = () -> IntStreamEx.range(100).boxed();
    checkCollector("1", IntStreamEx.range(1).boxed().toList(), s, MoreCollectors.least(1));
    checkCollector("2", IntStreamEx.range(2).boxed().toList(), s, MoreCollectors.least(2));
    checkCollector("10", IntStreamEx.range(10).boxed().toList(), s, MoreCollectors.least(10));
    checkCollector("100", IntStreamEx.range(100).boxed().toList(), s, MoreCollectors.least(100));
    checkCollector("200", IntStreamEx.range(100).boxed().toList(), s, MoreCollectors.least(200));
  }
 @Test
 public void testToArray() {
   List<String> input = Arrays.asList("a", "bb", "c", "", "cc", "eee", "bb", "ddd");
   for (StreamSupplier<String, StreamEx<String>> supplier : suppliers(() -> StreamEx.of(input))) {
     Map<Integer, String[]> result =
         supplier
             .get()
             .groupingBy(String::length, HashMap::new, MoreCollectors.toArray(String[]::new));
     assertArrayEquals(supplier.toString(), new String[] {""}, result.get(0));
     assertArrayEquals(supplier.toString(), new String[] {"a", "c"}, result.get(1));
     assertArrayEquals(supplier.toString(), new String[] {"bb", "cc", "bb"}, result.get(2));
     assertArrayEquals(supplier.toString(), new String[] {"eee", "ddd"}, result.get(3));
   }
 }
 @Test
 public void testIntersecting() {
   for (int i = 0; i < 5; i++) {
     List<List<String>> input =
         Arrays.asList(
             Arrays.asList("aa", "bb", "cc"),
             Arrays.asList("cc", "bb", "dd"),
             Arrays.asList("ee", "dd"),
             Arrays.asList("aa", "bb", "dd"));
     checkShortCircuitCollector(
         "#" + i, Collections.emptySet(), 3, input::stream, MoreCollectors.intersecting());
     List<List<Integer>> copies = new ArrayList<>(Collections.nCopies(100, Arrays.asList(1, 2)));
     checkShortCircuitCollector(
         "#" + i, StreamEx.of(1, 2).toSet(), 100, copies::stream, MoreCollectors.intersecting());
     copies.addAll(Collections.nCopies(100, Arrays.asList(3)));
     checkShortCircuitCollector(
         "#" + i, Collections.emptySet(), 101, copies::stream, MoreCollectors.intersecting());
     checkCollectorEmpty("#" + i, Collections.emptySet(), MoreCollectors.intersecting());
   }
 }
 @Test
 public void testHeadParallel() {
   List<Integer> expected = IntStreamEx.range(0, 2000, 2).boxed().toList();
   List<Integer> expectedShort = Arrays.asList(0, 1);
   for (int i = 0; i < 1000; i++) {
     assertEquals(
         "#" + i,
         expectedShort,
         IntStreamEx.range(1000).boxed().parallel().collect(MoreCollectors.head(2)));
     assertEquals(
         "#" + i,
         expected,
         IntStreamEx.range(10000)
             .boxed()
             .parallel()
             .filter(x -> x % 2 == 0)
             .collect(MoreCollectors.head(1000)));
   }
   assertEquals(
       expectedShort, StreamEx.iterate(0, x -> x + 1).parallel().collect(MoreCollectors.head(2)));
 }
  @Test
  public void testFlatMapping() {
    {
      Map<Integer, List<Integer>> expected =
          IntStreamEx.rangeClosed(1, 100)
              .boxed()
              .toMap(x -> IntStreamEx.rangeClosed(1, x).boxed().toList());
      Collector<Integer, ?, Map<Integer, List<Integer>>> groupingBy =
          Collectors.groupingBy(
              Function.identity(),
              MoreCollectors.flatMapping(
                  x -> IntStream.rangeClosed(1, x).boxed(), Collectors.toList()));
      checkCollector(
          "flatMappingSimple", expected, () -> IntStreamEx.rangeClosed(1, 100).boxed(), groupingBy);
    }

    Function<Entry<String, List<String>>, Stream<String>> valuesStream =
        e -> e.getValue() == null ? null : e.getValue().stream();
    List<Entry<String, List<String>>> list =
        EntryStream.of(
                "a", Arrays.asList("bb", "cc", "dd"), "b", Arrays.asList("ee", "ff"), "c", null)
            .append("c", Arrays.asList("gg"), "b", null, "a", Arrays.asList("hh"))
            .toList();
    {
      Map<String, List<String>> expected =
          EntryStream.of(list.stream())
              .flatMapValues(l -> l == null ? null : l.stream())
              .grouping();
      checkCollector(
          "flatMappingCombine",
          expected,
          list::stream,
          Collectors.groupingBy(
              Entry::getKey, MoreCollectors.flatMapping(valuesStream, Collectors.toList())));
      AtomicInteger openClose = new AtomicInteger();
      Collector<Entry<String, List<String>>, ?, Map<String, List<String>>> groupingBy =
          Collectors.groupingBy(
              Entry::getKey,
              MoreCollectors.flatMapping(
                  valuesStream.andThen(
                      s -> {
                        if (s == null) return null;
                        openClose.incrementAndGet();
                        return s.onClose(openClose::decrementAndGet);
                      }),
                  Collectors.toList()));
      checkCollector(
          "flatMappingCombineClosed",
          expected,
          list::stream,
          MoreCollectors.collectingAndThen(
              groupingBy,
              res -> {
                assertEquals(0, openClose.get());
                return res;
              }));
      boolean catched = false;
      try {
        Collector<Entry<String, List<String>>, ?, Map<String, List<String>>> groupingByException =
            Collectors.groupingBy(
                Entry::getKey,
                MoreCollectors.flatMapping(
                    valuesStream.andThen(
                        s -> {
                          if (s == null) return null;
                          openClose.incrementAndGet();
                          return s.onClose(openClose::decrementAndGet)
                              .peek(
                                  e -> {
                                    if (e.equals("gg")) throw new IllegalArgumentException(e);
                                  });
                        }),
                    Collectors.toList()));
        list.stream()
            .collect(
                MoreCollectors.collectingAndThen(
                    groupingByException,
                    res -> {
                      assertEquals(0, openClose.get());
                      return res;
                    }));
      } catch (IllegalArgumentException e1) {
        assertEquals("gg", e1.getMessage());
        catched = true;
      }
      assertTrue(catched);
    }
    {
      Map<String, List<String>> expected =
          EntryStream.of(
                  "a", Arrays.asList("bb"), "b", Arrays.asList("ee"), "c", Arrays.asList("gg"))
              .toMap();
      Collector<Entry<String, List<String>>, ?, List<String>> headOne =
          MoreCollectors.flatMapping(valuesStream, MoreCollectors.head(1));
      checkCollector(
          "flatMappingSubShort",
          expected,
          list::stream,
          Collectors.groupingBy(Entry::getKey, headOne));
      checkShortCircuitCollector(
          "flatMappingShort",
          expected,
          4,
          list::stream,
          MoreCollectors.groupingBy(Entry::getKey, StreamEx.of("a", "b", "c").toSet(), headOne));
      AtomicInteger cnt = new AtomicInteger();
      Collector<Entry<String, List<String>>, ?, List<String>> headPeek =
          MoreCollectors.flatMapping(
              valuesStream.andThen(s -> s == null ? null : s.peek(x -> cnt.incrementAndGet())),
              MoreCollectors.head(1));
      assertEquals(
          expected, StreamEx.of(list).collect(Collectors.groupingBy(Entry::getKey, headPeek)));
      assertEquals(3, cnt.get());
      cnt.set(0);
      assertEquals(
          expected,
          StreamEx.of(list)
              .collect(
                  MoreCollectors.groupingBy(
                      Entry::getKey, StreamEx.of("a", "b", "c").toSet(), headPeek)));
      assertEquals(3, cnt.get());
    }
    {
      Map<String, List<String>> expected =
          EntryStream.of(
                  "a",
                  Arrays.asList("bb", "cc"),
                  "b",
                  Arrays.asList("ee", "ff"),
                  "c",
                  Arrays.asList("gg"))
              .toMap();
      Collector<Entry<String, List<String>>, ?, List<String>> headTwo =
          MoreCollectors.flatMapping(valuesStream, MoreCollectors.head(2));
      checkCollector(
          "flatMappingSubShort",
          expected,
          list::stream,
          Collectors.groupingBy(Entry::getKey, headTwo));
      AtomicInteger openClose = new AtomicInteger();
      boolean catched = false;
      try {
        Collector<Entry<String, List<String>>, ?, Map<String, List<String>>> groupingByException =
            Collectors.groupingBy(
                Entry::getKey,
                MoreCollectors.flatMapping(
                    valuesStream.andThen(
                        s -> {
                          if (s == null) return null;
                          openClose.incrementAndGet();
                          return s.onClose(openClose::decrementAndGet)
                              .peek(
                                  e -> {
                                    if (e.equals("gg")) throw new IllegalArgumentException(e);
                                  });
                        }),
                    MoreCollectors.head(2)));
        list.stream()
            .collect(
                MoreCollectors.collectingAndThen(
                    groupingByException,
                    res -> {
                      assertEquals(0, openClose.get());
                      return res;
                    }));
      } catch (IllegalArgumentException e1) {
        assertEquals("gg", e1.getMessage());
        catched = true;
      }
      assertTrue(catched);
    }
  }