Search in sources :

Example 1 with MutableMap

use of org.eclipse.collections.api.map.MutableMap in project eclipse-collections by eclipse.

the class SerialParallelLazyPerformanceTest method forEach.

private void forEach(FastList<Integer> collection) {
    MutableList<Runnable> runnables = FastList.newList();
    runnables.add(() -> this.basicSerialForEachPerformance(collection, SERIAL_RUN_COUNT));
    int cores = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(cores);
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicParallelLazyForEachPerformance(collection, "Lambda", item -> map.put(item, Boolean.TRUE), PARALLEL_RUN_COUNT, cores, service);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicParallelLazyForEachPerformance(collection, "Procedure", (Procedure<Integer>) each -> map.put(each, Boolean.TRUE), PARALLEL_RUN_COUNT, cores, service);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(collection, "Lambda", item -> map.put(item, Boolean.TRUE), PARALLEL_RUN_COUNT);
    });
    List<Integer> arrayList = new ArrayList<>(collection);
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(arrayList, "Lambda", item -> map.put(item, Boolean.TRUE), PARALLEL_RUN_COUNT);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(arrayList, "Consumer", each -> map.put(each, Boolean.TRUE), PARALLEL_RUN_COUNT);
    });
    this.shuffleAndRun(runnables);
    service.shutdown();
    try {
        service.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : Arrays(java.util.Arrays) ConcurrentHashMap(org.eclipse.collections.impl.map.mutable.ConcurrentHashMap) Function(org.eclipse.collections.api.block.function.Function) Predicate(org.eclipse.collections.api.block.predicate.Predicate) LoggerFactory(org.slf4j.LoggerFactory) Procedure(org.eclipse.collections.api.block.procedure.Procedure) Verify(org.eclipse.collections.impl.test.Verify) MutableList(org.eclipse.collections.api.list.MutableList) NumberFormat(java.text.NumberFormat) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Function0(org.eclipse.collections.api.block.function.Function0) MutableMap(org.eclipse.collections.api.map.MutableMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Interval(org.eclipse.collections.impl.list.Interval) ExecutorService(java.util.concurrent.ExecutorService) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) ParallelTests(org.eclipse.collections.impl.ParallelTests) Logger(org.slf4j.Logger) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) Collection(java.util.Collection) Set(java.util.Set) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) IntInterval(org.eclipse.collections.impl.list.primitive.IntInterval) Assert(org.junit.Assert) IntProcedure(org.eclipse.collections.api.block.procedure.primitive.IntProcedure) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentHashMap(org.eclipse.collections.impl.map.mutable.ConcurrentHashMap)

Example 2 with MutableMap

use of org.eclipse.collections.api.map.MutableMap in project eclipse-collections by eclipse.

the class ParallelIterateTest method sumByBigInteger.

@Test
public void sumByBigInteger() {
    MutableList<BigInteger> list = Interval.oneTo(100000).collect(Object::toString).collect(BigInteger::new).toList().shuffleThis();
    MutableMap<String, BigInteger> sumByCount = ParallelIterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> BigInteger.valueOf(1L));
    Assert.assertEquals(BigInteger.valueOf(50000L), sumByCount.get("Even"));
    Assert.assertEquals(BigInteger.valueOf(50000L), sumByCount.get("Odd"));
    MutableMap<String, BigInteger> sumByValue = ParallelIterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> bi);
    Assert.assertEquals(Iterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> bi), sumByValue);
    MutableMap<Integer, BigInteger> sumByValue2 = ParallelIterate.sumByBigInteger(list, bi -> bi.intValue() % 1000, bi -> bi);
    Assert.assertEquals(Iterate.sumByBigInteger(list, bi -> bi.intValue() % 1000, bi -> bi), sumByValue2);
    MutableList<BigInteger> list2 = Interval.oneTo(UNEVEN_COUNT_FOR_SUMBY).collect(Object::toString).collect(BigInteger::new).toList();
    MutableMap<String, BigInteger> sumByValue3 = ParallelIterate.sumByBigInteger(list2, EVEN_OR_ODD_BI, bi -> bi);
    Assert.assertEquals(Iterate.sumByBigInteger(list2, EVEN_OR_ODD_BI, bi -> bi), sumByValue3);
    MutableMap<Integer, BigInteger> sumByValue4 = ParallelIterate.sumByBigInteger(list2, bi -> bi.intValue() % 1000, bi -> bi);
    Assert.assertEquals(Iterate.sumByBigInteger(list2, bi -> bi.intValue() % 1000, bi -> bi), sumByValue4);
    Interval small = Interval.oneTo(11);
    MutableMap<String, BigInteger> smallSumByCount = ParallelIterate.sumByBigInteger(small, EVEN_OR_ODD, i -> BigInteger.valueOf(1L));
    Assert.assertEquals(new BigInteger("5"), smallSumByCount.get("Even"));
    Assert.assertEquals(new BigInteger("6"), smallSumByCount.get("Odd"));
}
Also used : Arrays(java.util.Arrays) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Multimap(org.eclipse.collections.api.multimap.Multimap) CompositeFastList(org.eclipse.collections.impl.list.mutable.CompositeFastList) HashingStrategies(org.eclipse.collections.impl.block.factory.HashingStrategies) Verify(org.eclipse.collections.impl.test.Verify) StringFunctions(org.eclipse.collections.impl.block.factory.StringFunctions) MutableList(org.eclipse.collections.api.list.MutableList) UnifiedSetWithHashingStrategy(org.eclipse.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy) BigDecimal(java.math.BigDecimal) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Functions(org.eclipse.collections.impl.block.factory.Functions) After(org.junit.After) MutableMultimap(org.eclipse.collections.api.multimap.MutableMultimap) Interval(org.eclipse.collections.impl.list.Interval) BigInteger(java.math.BigInteger) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Collection(java.util.Collection) ObjectDoubleMap(org.eclipse.collections.api.map.primitive.ObjectDoubleMap) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Iterate(org.eclipse.collections.impl.utility.Iterate) Executors(java.util.concurrent.Executors) ListAdapter(org.eclipse.collections.impl.list.mutable.ListAdapter) List(java.util.List) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) ObjectLongMap(org.eclipse.collections.api.map.primitive.ObjectLongMap) Lists(org.eclipse.collections.impl.factory.Lists) Function(org.eclipse.collections.api.block.function.Function) Bag(org.eclipse.collections.api.bag.Bag) Procedure(org.eclipse.collections.api.block.procedure.Procedure) ArrayListAdapter(org.eclipse.collections.impl.list.mutable.ArrayListAdapter) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableMap(org.eclipse.collections.api.map.MutableMap) HashBagMultimap(org.eclipse.collections.impl.multimap.bag.HashBagMultimap) SynchronizedPutUnifiedSetMultimap(org.eclipse.collections.impl.multimap.set.SynchronizedPutUnifiedSetMultimap) Function2(org.eclipse.collections.api.block.function.Function2) SynchronizedPutHashBagMultimap(org.eclipse.collections.impl.multimap.bag.SynchronizedPutHashBagMultimap) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) Test(org.junit.Test) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) ImmutableList(org.eclipse.collections.api.list.ImmutableList) MapIterable(org.eclipse.collections.api.map.MapIterable) Assert(org.junit.Assert) Collections(java.util.Collections) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 3 with MutableMap

use of org.eclipse.collections.api.map.MutableMap in project eclipse-collections by eclipse.

the class ParallelIterateTest method sumByBigDecimal.

@Test
public void sumByBigDecimal() {
    MutableList<BigDecimal> list = Interval.oneTo(100000).collect(BigDecimal::new).toList().shuffleThis();
    MutableMap<String, BigDecimal> sumByCount = ParallelIterate.sumByBigDecimal(list, EVEN_OR_ODD_BD, bd -> new BigDecimal(1L));
    Assert.assertEquals(BigDecimal.valueOf(50000L), sumByCount.get("Even"));
    Assert.assertEquals(BigDecimal.valueOf(50000L), sumByCount.get("Odd"));
    MutableMap<String, BigDecimal> sumByValue = ParallelIterate.sumByBigDecimal(list, EVEN_OR_ODD_BD, bd -> bd);
    Assert.assertEquals(Iterate.sumByBigDecimal(list, EVEN_OR_ODD_BD, bd -> bd), sumByValue);
    MutableMap<Integer, BigDecimal> sumByValue2 = ParallelIterate.sumByBigDecimal(list, bd -> bd.intValue() % 1000, bd -> bd);
    Assert.assertEquals(Iterate.sumByBigDecimal(list, bd -> bd.intValue() % 1000, bd -> bd), sumByValue2);
    MutableList<BigDecimal> list2 = Interval.oneTo(UNEVEN_COUNT_FOR_SUMBY).collect(BigDecimal::new).toList();
    MutableMap<String, BigDecimal> sumByValue3 = ParallelIterate.sumByBigDecimal(list2, EVEN_OR_ODD_BD, bd -> bd);
    Assert.assertEquals(Iterate.sumByBigDecimal(list2, EVEN_OR_ODD_BD, bd -> bd), sumByValue3);
    MutableMap<Integer, BigDecimal> sumByValue4 = ParallelIterate.sumByBigDecimal(list2, bd -> bd.intValue() % 1000, bd -> bd);
    Assert.assertEquals(Iterate.sumByBigDecimal(list2, bd -> bd.intValue() % 1000, bd -> bd), sumByValue4);
    Interval small = Interval.oneTo(11);
    MutableMap<String, BigDecimal> smallSumByCount = ParallelIterate.sumByBigDecimal(small, EVEN_OR_ODD, i -> BigDecimal.valueOf(1L));
    Assert.assertEquals(new BigDecimal(5), smallSumByCount.get("Even"));
    Assert.assertEquals(new BigDecimal(6), smallSumByCount.get("Odd"));
}
Also used : Arrays(java.util.Arrays) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Multimap(org.eclipse.collections.api.multimap.Multimap) CompositeFastList(org.eclipse.collections.impl.list.mutable.CompositeFastList) HashingStrategies(org.eclipse.collections.impl.block.factory.HashingStrategies) Verify(org.eclipse.collections.impl.test.Verify) StringFunctions(org.eclipse.collections.impl.block.factory.StringFunctions) MutableList(org.eclipse.collections.api.list.MutableList) UnifiedSetWithHashingStrategy(org.eclipse.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy) BigDecimal(java.math.BigDecimal) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Functions(org.eclipse.collections.impl.block.factory.Functions) After(org.junit.After) MutableMultimap(org.eclipse.collections.api.multimap.MutableMultimap) Interval(org.eclipse.collections.impl.list.Interval) BigInteger(java.math.BigInteger) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Collection(java.util.Collection) ObjectDoubleMap(org.eclipse.collections.api.map.primitive.ObjectDoubleMap) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Iterate(org.eclipse.collections.impl.utility.Iterate) Executors(java.util.concurrent.Executors) ListAdapter(org.eclipse.collections.impl.list.mutable.ListAdapter) List(java.util.List) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) ObjectLongMap(org.eclipse.collections.api.map.primitive.ObjectLongMap) Lists(org.eclipse.collections.impl.factory.Lists) Function(org.eclipse.collections.api.block.function.Function) Bag(org.eclipse.collections.api.bag.Bag) Procedure(org.eclipse.collections.api.block.procedure.Procedure) ArrayListAdapter(org.eclipse.collections.impl.list.mutable.ArrayListAdapter) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableMap(org.eclipse.collections.api.map.MutableMap) HashBagMultimap(org.eclipse.collections.impl.multimap.bag.HashBagMultimap) SynchronizedPutUnifiedSetMultimap(org.eclipse.collections.impl.multimap.set.SynchronizedPutUnifiedSetMultimap) Function2(org.eclipse.collections.api.block.function.Function2) SynchronizedPutHashBagMultimap(org.eclipse.collections.impl.multimap.bag.SynchronizedPutHashBagMultimap) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) Test(org.junit.Test) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) ImmutableList(org.eclipse.collections.api.list.ImmutableList) MapIterable(org.eclipse.collections.api.map.MapIterable) Assert(org.junit.Assert) Collections(java.util.Collections) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 4 with MutableMap

use of org.eclipse.collections.api.map.MutableMap in project eclipse-collections by eclipse.

the class IterateTest method injectIntoIfProcedure.

@Test
public void injectIntoIfProcedure() {
    Integer newItemToIndex = 99;
    MutableMap<String, Integer> index1 = IterateTest.createPretendIndex(1);
    MutableMap<String, Integer> index2 = IterateTest.createPretendIndex(2);
    MutableMap<String, Integer> index3 = IterateTest.createPretendIndex(3);
    MutableMap<String, Integer> index4 = IterateTest.createPretendIndex(4);
    MutableMap<String, Integer> index5 = IterateTest.createPretendIndex(5);
    MutableMap<String, MutableMap<String, Integer>> allIndexes = UnifiedMap.newMapWith(Tuples.pair("pretend index 1", index1), Tuples.pair("pretend index 2", index2), Tuples.pair("pretend index 3", index3), Tuples.pair("pretend index 4", index4), Tuples.pair("pretend index 5", index5));
    MutableSet<MutableMap<String, Integer>> systemIndexes = Sets.fixedSize.of(index3, index5);
    MapIterate.injectIntoIf(newItemToIndex, allIndexes, Predicates.notIn(systemIndexes), (itemToAdd, index) -> {
        index.put(itemToAdd.toString(), itemToAdd);
        return itemToAdd;
    });
    Verify.assertSize(5, allIndexes);
    Verify.assertContainsKey("pretend index 2", allIndexes);
    Verify.assertContainsKey("pretend index 3", allIndexes);
    Verify.assertContainsKeyValue("99", newItemToIndex, index1);
    Verify.assertContainsKeyValue("99", newItemToIndex, index2);
    Verify.assertNotContainsKey("99", index3);
    Verify.assertContainsKeyValue("99", newItemToIndex, index4);
    Verify.assertNotContainsKey("99", index5);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) MutableMap(org.eclipse.collections.api.map.MutableMap) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 MutableMap (org.eclipse.collections.api.map.MutableMap)4 Test (org.junit.Test)4 BigInteger (java.math.BigInteger)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 List (java.util.List)3 ExecutorService (java.util.concurrent.ExecutorService)3 Executors (java.util.concurrent.Executors)3 Function (org.eclipse.collections.api.block.function.Function)3 Predicate (org.eclipse.collections.api.block.predicate.Predicate)3 Procedure (org.eclipse.collections.api.block.procedure.Procedure)3 MutableList (org.eclipse.collections.api.list.MutableList)3 Predicates (org.eclipse.collections.impl.block.factory.Predicates)3 Procedures (org.eclipse.collections.impl.block.factory.Procedures)3 Interval (org.eclipse.collections.impl.list.Interval)3 BigDecimal (java.math.BigDecimal)2 Collections (java.util.Collections)2 LinkedList (java.util.LinkedList)2