@Test
  public void testForEachUsingMap() {
    // Test the default batch size calculations
    IntegerSum sum1 = new IntegerSum(0);
    MutableMap<String, Integer> map1 =
        Interval.fromTo(1, 10000).toMap(String::valueOf, Functions.getIntegerPassThru());
    ParallelIterate.forEach(map1, new SumProcedure(sum1), new SumCombiner(sum1));
    Assert.assertEquals(50005000, sum1.getSum());

    // Testing batch size 1
    IntegerSum sum2 = new IntegerSum(0);
    UnifiedMap<String, Integer> map2 =
        (UnifiedMap<String, Integer>)
            Interval.fromTo(1, 100).toMap(String::valueOf, Functions.getIntegerPassThru());
    ParallelIterate.forEach(
        map2, new SumProcedure(sum2), new SumCombiner(sum2), 1, map2.getBatchCount(map2.size()));
    Assert.assertEquals(5050, sum2.getSum());

    // Testing an uneven batch size
    IntegerSum sum3 = new IntegerSum(0);
    UnifiedMap<String, Integer> set3 =
        (UnifiedMap<String, Integer>)
            Interval.fromTo(1, 100).toMap(String::valueOf, Functions.getIntegerPassThru());
    ParallelIterate.forEach(
        set3, new SumProcedure(sum3), new SumCombiner(sum3), 1, set3.getBatchCount(13));
    Assert.assertEquals(5050, sum3.getSum());
  }
  @Test
  public void groupByEach() {
    MutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3);

    NegativeIntervalFunction function = new NegativeIntervalFunction();
    MutableMultimap<Integer, Integer> expected = FastListMultimap.newMultimap();
    for (int i = 1; i < map.size(); i++) {
      expected.putAll(-i, Interval.fromTo(i, map.size()));
    }

    Multimap<Integer, Integer> actual = map.groupByEach(function);
    expected.forEachKey(
        each -> {
          Assert.assertTrue(actual.containsKey(each));
          MutableList<Integer> values = actual.get(each).toList();
          Verify.assertNotEmpty(values);
          Assert.assertTrue(expected.get(each).containsAllIterable(values));
        });

    Multimap<Integer, Integer> actualFromTarget =
        map.groupByEach(function, FastListMultimap.<Integer, Integer>newMultimap());
    expected.forEachKey(
        each -> {
          Assert.assertTrue(actualFromTarget.containsKey(each));
          MutableList<Integer> values = actualFromTarget.get(each).toList();
          Verify.assertNotEmpty(values);
          Assert.assertTrue(expected.get(each).containsAllIterable(values));
        });
  }
 @Override
 @Test
 public void forEach() {
   super.forEach();
   MutableList<Integer> list = FastList.newList();
   CompositeFastList<Integer> iterables = new CompositeFastList<Integer>();
   iterables.addComposited(Interval.oneTo(5).toList());
   iterables.addComposited(Interval.fromTo(6, 10).toList());
   iterables.forEach(CollectionAddProcedure.on(list));
   Verify.assertSize(10, list);
   Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11)));
 }
Exemplo n.º 4
0
  @Test
  public void toSortedMap_with_comparator() {
    MutableSortedMap<Integer, String> map =
        this.newBag()
            .toSortedMap(
                Comparators.<Integer>reverseNaturalOrder(),
                Integer::valueOf,
                Functions.<String>getPassThru());

    Verify.assertMapsEqual(
        this.newBag().toMap(Integer::valueOf, Functions.<String>getPassThru()), map);
    Verify.assertListsEqual(Interval.fromTo(this.numKeys(), 1), map.keySet().toList());
  }
 @Override
 @Test
 public void forEachWith() {
   super.forEachWith();
   MutableList<Integer> list = FastList.newList();
   CompositeFastList<Integer> iterables = new CompositeFastList<Integer>();
   iterables.addComposited(Interval.fromTo(6, 10).toList());
   iterables.addComposited(Interval.oneTo(5).toList());
   iterables.forEachWith((each, parameter) -> list.add(parameter.intValue(), each), 0);
   Verify.assertSize(10, list);
   Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11)));
   Verify.assertStartsWith(list, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6);
 }
 @Override
 @Test
 public void forEachWithIndex() {
   super.forEachWithIndex();
   MutableList<Integer> list = FastList.newList();
   CompositeFastList<Integer> iterables = new CompositeFastList<Integer>();
   iterables.addComposited(Interval.fromTo(6, 10).toList());
   iterables.addComposited(Interval.oneTo(5).toList());
   iterables.forEachWithIndex((each, index) -> list.add(index, each));
   Verify.assertSize(10, list);
   Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11)));
   Verify.assertStartsWith(list, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5);
 }
  @Test
  public void groupByEach() {
    MutableMultimap<Integer, Integer> expected = FastListMultimap.newMultimap();
    for (int i = 1; i < 8; i++) {
      expected.putAll(-i, Interval.fromTo(i, 7));
    }

    Multimap<Integer, Integer> actual =
        this.lazyIterable.groupByEach(new NegativeIntervalFunction());
    Assert.assertEquals(expected, actual);

    Multimap<Integer, Integer> actualWithTarget =
        this.lazyIterable.groupByEach(
            new NegativeIntervalFunction(), FastListMultimap.<Integer, Integer>newMultimap());
    Assert.assertEquals(expected, actualWithTarget);
  }
Exemplo n.º 8
0
  @Test
  public void groupByEach() {
    ImmutableBag<Integer> immutableBag = this.newBag().collect(Integer::valueOf);

    MutableMultimap<Integer, Integer> expected = HashBagMultimap.newMultimap();
    int keys = this.numKeys();
    immutableBag.forEachWithOccurrences(
        (each, parameter) -> {
          HashBag<Integer> bag = HashBag.newBag();
          Interval.fromTo(each, keys)
              .forEach((int eachInt) -> bag.addOccurrences(eachInt, eachInt));
          expected.putAll(-each, bag);
        });
    Multimap<Integer, Integer> actual = immutableBag.groupByEach(new NegativeIntervalFunction());
    Assert.assertEquals(expected, actual);

    Multimap<Integer, Integer> actualWithTarget =
        immutableBag.groupByEach(
            new NegativeIntervalFunction(), HashBagMultimap.<Integer, Integer>newMultimap());
    Assert.assertEquals(expected, actualWithTarget);
  }