Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Eclipse Collection Factory Examples - Object and Primitive
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.ImmutableIntBag;
import org.eclipse.collections.api.bag.primitive.IntBag;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.ImmutableIntList;
import org.eclipse.collections.api.list.primitive.IntList;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.ImmutableIntIntMap;
import org.eclipse.collections.api.map.primitive.IntIntMap;
import org.eclipse.collections.api.map.primitive.MutableIntIntMap;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.SetIterable;
import org.eclipse.collections.api.set.primitive.ImmutableIntSet;
import org.eclipse.collections.api.set.primitive.IntSet;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.api.stack.ImmutableStack;
import org.eclipse.collections.api.stack.MutableStack;
import org.eclipse.collections.api.stack.StackIterable;
import org.eclipse.collections.api.stack.primitive.ImmutableIntStack;
import org.eclipse.collections.api.stack.primitive.IntStack;
import org.eclipse.collections.api.stack.primitive.MutableIntStack;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.factory.Stacks;
import org.eclipse.collections.impl.factory.primitive.IntBags;
import org.eclipse.collections.impl.factory.primitive.IntIntMaps;
import org.eclipse.collections.impl.factory.primitive.IntLists;
import org.eclipse.collections.impl.factory.primitive.IntSets;
import org.eclipse.collections.impl.factory.primitive.IntStacks;
import org.junit.Assert;
import org.junit.Test;
public class EclipseCollectionFactories
{
@Test
public void mutableObject()
{
MutableList<Integer> list = Lists.mutable.with(1, 2, 3);
MutableSet<Integer> set = Sets.mutable.with(1, 2, 3);
MutableBag<Integer> bag = Bags.mutable.with(1, 2, 3);
MutableStack<Integer> stack = Stacks.mutable.with(1, 2, 3);
MutableMap<Integer, Integer> map = Maps.mutable.with(1, 1, 2, 2, 3, 3);
this.assertObjectValuesEqual(list, set, bag, stack, map);
}
@Test
public void immutableObject()
{
ImmutableList<Integer> list = Lists.immutable.with(1, 2, 3);
ImmutableSet<Integer> set = Sets.immutable.with(1, 2, 3);
ImmutableBag<Integer> bag = Bags.immutable.with(1, 2, 3);
ImmutableStack<Integer> stack = Stacks.immutable.with(1, 2, 3);
ImmutableMap<Integer, Integer> map = Maps.immutable.with(1, 1, 2, 2, 3, 3);
this.assertObjectValuesEqual(list, set, bag, stack, map);
}
@Test
public void mutablePrimitive()
{
MutableIntList list = IntLists.mutable.with(1, 2, 3);
MutableIntSet set = IntSets.mutable.with(1, 2, 3);
MutableIntBag bag = IntBags.mutable.with(1, 2, 3);
MutableIntStack stack = IntStacks.mutable.with(1, 2, 3);
MutableIntIntMap map = IntIntMaps.mutable.empty()
.withKeyValue(1, 1)
.withKeyValue(2, 2)
.withKeyValue(3, 3);
this.assertPrimitiveValuesEqual(list, set, bag, stack, map);
}
@Test
public void immutablePrimitive()
{
ImmutableIntList list = IntLists.immutable.with(1, 2, 3);
ImmutableIntSet set = IntSets.immutable.with(1, 2, 3);
ImmutableIntBag bag = IntBags.immutable.with(1, 2, 3);
ImmutableIntStack stack = IntStacks.immutable.with(1, 2, 3);
ImmutableIntIntMap map = IntIntMaps.mutable.empty()
.withKeyValue(1, 1)
.withKeyValue(2, 2)
.withKeyValue(3, 3)
.toImmutable();
this.assertPrimitiveValuesEqual(list, set, bag, stack, map);
}
private void assertObjectValuesEqual(
ListIterable<Integer> list,
SetIterable<Integer> set,
Bag<Integer> bag,
StackIterable<Integer> stack,
MapIterable<Integer, Integer> map)
{
MutableBag<Integer> expected = Bags.mutable.with(1, 2, 3);
Assert.assertEquals(expected, list.toBag());
Assert.assertEquals(expected, set.toBag());
Assert.assertEquals(expected, bag);
Assert.assertEquals(expected, stack.toBag());
Assert.assertEquals(expected, map.toBag());
}
private void assertPrimitiveValuesEqual(
IntList list,
IntSet set,
IntBag bag,
IntStack stack,
IntIntMap map)
{
IntBag expected = IntBags.mutable.with(1, 2, 3);
Assert.assertEquals(expected, list.toBag());
Assert.assertEquals(expected, set.toBag());
Assert.assertEquals(expected, bag);
Assert.assertEquals(expected, stack.toBag());
Assert.assertEquals(expected, map.toBag());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.