コード例 #1
0
  @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());
  }
コード例 #2
0
 @Test
 public void testPartitioningBy() {
   Collector<Integer, ?, Map<Boolean, Optional<Integer>>> by20 =
       MoreCollectors.partitioningBy(x -> x % 20 == 0, MoreCollectors.first());
   Collector<Integer, ?, Map<Boolean, Optional<Integer>>> by200 =
       MoreCollectors.partitioningBy(x -> x % 200 == 0, MoreCollectors.first());
   Supplier<Stream<Integer>> supplier = () -> IntStreamEx.range(1, 100).boxed();
   checkShortCircuitCollector(
       "by20", new BooleanMap<>(Optional.of(20), Optional.of(1)), 20, supplier, by20);
   checkShortCircuitCollector(
       "by200", new BooleanMap<>(Optional.empty(), Optional.of(1)), 99, supplier, by200);
 }
コード例 #3
0
 @Test
 public void testFiltering() {
   Collector<Integer, ?, Optional<Integer>> firstEven =
       MoreCollectors.filtering(x -> x % 2 == 0, MoreCollectors.first());
   Collector<Integer, ?, Optional<Integer>> firstOdd =
       MoreCollectors.filtering(x -> x % 2 != 0, MoreCollectors.first());
   Collector<Integer, ?, Integer> sumOddEven =
       MoreCollectors.pairing(firstEven, firstOdd, (e, o) -> e.get() + o.get());
   List<Integer> ints = Arrays.asList(1, 3, 5, 7, 9, 10, 8, 6, 4, 2, 3, 7, 11);
   checkShortCircuitCollector("sumOddEven", 11, 6, ints::stream, sumOddEven);
   Collector<Integer, ?, Long> countEven =
       MoreCollectors.filtering(x -> x % 2 == 0, Collectors.counting());
   checkCollector("filtering", 5L, ints::stream, countEven);
 }
コード例 #4
0
  @Test
  public void testMapping() {
    List<String> input = Arrays.asList("Capital", "lower", "Foo", "bar");
    Collector<String, ?, Map<Boolean, Optional<Integer>>> collector =
        MoreCollectors.partitioningBy(
            str -> Character.isUpperCase(str.charAt(0)),
            MoreCollectors.mapping(String::length, MoreCollectors.first()));
    checkShortCircuitCollector(
        "mapping", new BooleanMap<>(Optional.of(7), Optional.of(5)), 2, input::stream, collector);
    Collector<String, ?, Map<Boolean, Optional<Integer>>> collectorLast =
        MoreCollectors.partitioningBy(
            str -> Character.isUpperCase(str.charAt(0)),
            MoreCollectors.mapping(String::length, MoreCollectors.last()));
    checkCollector(
        "last", new BooleanMap<>(Optional.of(3), Optional.of(3)), input::stream, collectorLast);

    input = Arrays.asList("Abc", "Bac", "Aac", "Abv", "Bbc", "Bgd", "Atc", "Bpv");
    Map<Character, List<String>> expected =
        EntryStream.of('A', Arrays.asList("Abc", "Aac"), 'B', Arrays.asList("Bac", "Bbc")).toMap();
    AtomicInteger cnt = new AtomicInteger();
    Collector<String, ?, Map<Character, List<String>>> groupMap =
        Collectors.groupingBy(
            s -> s.charAt(0),
            MoreCollectors.mapping(
                x -> {
                  cnt.incrementAndGet();
                  return x;
                },
                MoreCollectors.head(2)));
    checkCollector("groupMap", expected, input::stream, groupMap);
    cnt.set(0);
    assertEquals(expected, input.stream().collect(groupMap));
    assertEquals(4, cnt.get());
  }
コード例 #5
0
  @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());
  }
コード例 #6
0
  @Test
  public void testMaxAll() {
    List<String> input = Arrays.asList("a", "bb", "c", "", "cc", "eee", "bb", "ddd");
    checkCollector(
        "maxAll",
        Arrays.asList("eee", "ddd"),
        input::stream,
        MoreCollectors.maxAll(Comparator.comparingInt(String::length)));
    Collector<String, ?, String> maxAllJoin =
        MoreCollectors.maxAll(Comparator.comparingInt(String::length), Collectors.joining(","));
    checkCollector("maxAllJoin", "eee,ddd", input::stream, maxAllJoin);
    checkCollector(
        "minAll",
        1L,
        input::stream,
        MoreCollectors.minAll(Comparator.comparingInt(String::length), Collectors.counting()));
    checkCollector(
        "minAllEmpty",
        Arrays.asList(""),
        input::stream,
        MoreCollectors.minAll(Comparator.comparingInt(String::length)));
    checkCollectorEmpty(
        "maxAll",
        Collections.emptyList(),
        MoreCollectors.maxAll(Comparator.comparingInt(String::length)));
    checkCollectorEmpty("maxAllJoin", "", maxAllJoin);

    List<Integer> ints = IntStreamEx.of(new Random(1), 10000, 1, 1000).boxed().toList();
    List<Integer> expectedMax = getMaxAll(ints, Comparator.naturalOrder());
    List<Integer> expectedMin = getMaxAll(ints, Comparator.reverseOrder());
    Collector<Integer, ?, SimpleEntry<Integer, Long>> downstream =
        MoreCollectors.pairing(
            MoreCollectors.first(),
            Collectors.counting(),
            (opt, cnt) -> new AbstractMap.SimpleEntry<>(opt.get(), cnt));

    checkCollector("maxAll", expectedMax, ints::stream, MoreCollectors.maxAll(Integer::compare));
    checkCollector("minAll", expectedMin, ints::stream, MoreCollectors.minAll());
    checkCollector(
        "entry",
        new SimpleEntry<>(expectedMax.get(0), (long) expectedMax.size()),
        ints::stream,
        MoreCollectors.maxAll(downstream));
    checkCollector(
        "entry",
        new SimpleEntry<>(expectedMin.get(0), (long) expectedMin.size()),
        ints::stream,
        MoreCollectors.minAll(downstream));

    Integer a = new Integer(1), b = new Integer(1), c = new Integer(1000), d = new Integer(1000);
    ints = IntStreamEx.range(10, 100).boxed().append(a, c).prepend(b, d).toList();
    for (StreamExSupplier<Integer> supplier : streamEx(ints::stream)) {
      List<Integer> list = supplier.get().collect(MoreCollectors.maxAll());
      assertEquals(2, list.size());
      assertSame(d, list.get(0));
      assertSame(c, list.get(1));

      list = supplier.get().collect(MoreCollectors.minAll());
      assertEquals(2, list.size());
      assertSame(b, list.get(0));
      assertSame(a, list.get(1));
    }
  }