3.2.0 (June 2013)
Binaries
Javadoc
New Functionality
Immutable Primitive Containers
Version 3.2 adds initial support for immutable primitive containers, starting with lists. They can be created using the toImmutable()
method or using the primitive list factories.
MutableIntList mutableIntList = IntArrayList.newListWith(1, 2, 3);
ImmutableIntList immutableIntList = mutableIntList.toImmutable();
ImmutableIntList immutableIntList = IntLists.immutable.of(1, 2, 3);
injectInto() on primitive collections
Primitive containers now support injectInto()
. It works like RichIterable.injectInto()
, but it takes an Object<Primitive>ToObjectFunction
, where the primitive type matches the container type. The accumulator can be any type.
IntArrayList arrayList = IntArrayList.newListWith(1, 2, 3);
MutableInteger sum = arrayList.injectInto(new MutableInteger(0), new ObjectIntToObjectFunction<MutableInteger, MutableInteger>()
{
public MutableInteger valueOf(MutableInteger object, int value)
{
return object.add(value);
}
});
Assert.assertEquals(new MutableInteger(6), sum);
injectIntoWithIndex() on primitive lists
injectIntoWithIndex()
is similar to injectInto()
, except the function takes a third parameter: the index of the current element. It's only available on lists since sets and bags don't have indexed positions.
IntArrayList list1 = IntArrayList.newListWith(1, 2, 3);
final IntArrayList list2 = IntArrayList.newListWith(1, 2, 3);
MutableInteger dotProduct = list1.injectIntoWithIndex(new MutableInteger(0), new ObjectIntIntToObjectFunction<MutableInteger, MutableInteger>()
{
public MutableInteger valueOf(MutableInteger object, int value, int index)
{
return object.add(value * list2.get(index));
}
});
Assert.assertEquals(new MutableInteger(14), dotProduct);
dotProduct() on primitive lists
The primitive lists that hold numeric types now support dotProduct()
. It takes a second list, multiplies the pairs of elements appearing at the same index, and sums the products.
@Test
public void dotProduct()
{
IntArrayList list1 = IntArrayList.newListWith(1, 2);
IntArrayList list2 = IntArrayList.newListWith(3, 4);
Assert.assertEquals(1 * 3 + 2 * 4, list1.dotProduct(list2));
}
forEachWithIndex() on primitive lists
Evaluates the <Primitive>IntProcedure
for each element in the list passing in the element and its index.
final StringBuilder string = new StringBuilder();
CharArrayList.newListWith('a', 'b', 'c').forEachWithIndex(new CharIntProcedure()
{
public void value(char each, int index)
{
string.append(each).append('-').append(index);
}
});
Assert.assertEquals("a-1b-2c-3", string.toString());
Unmodifiable wrappers for primitive maps
Version 3.2 adds unmodifiable wrappers for primitive-to-Object maps and Object-to-primitive maps. With this addition, all primitive collections support asUnmodifiable()
.
@Test(expected = UnsupportedOperationException.class)
public void put_throws()
{
MutableIntObjectMap<String> map = IntObjectHashMap.newWithKeysValues(0, "zero", 31, "thirtyOne", 32, "thirtyTwo");
MutableIntObjectMap<String> unmodifiableMap = map.asUnmodifiable();
unmodifiableMap.put(0, "one");
}
No API changes in this release
This release preserves binary, source, and serialization compatibility with 3.0.
- The method toImmutable() already existed on primitive collections in GS Collections 3.0, but it threw UnsupportedOperationException.
- injectInto(), injectIntoWithIndex() and forEachWithIndex() only have been added to the implementations. They will be added to the primitive iterable interfaces in the next major release.