Skip to content

4.2.0 (October 2013)

Compare
Choose a tag to compare
@goldmansachs goldmansachs released this 28 Oct 22:25
· 489 commits to master since this release

Acquiring GS Collections

Maven

<dependency>
  <groupId>com.goldmansachs</groupId>
  <artifactId>gs-collections-api</artifactId>
  <version>4.2.0</version>
</dependency>

<dependency>
  <groupId>com.goldmansachs</groupId>
  <artifactId>gs-collections</artifactId>
  <version>4.2.0</version>
</dependency>

<dependency>
  <groupId>com.goldmansachs</groupId>
  <artifactId>gs-collections-testutils</artifactId>
  <version>4.2.0</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>com.goldmansachs</groupId>
  <artifactId>gs-collections-forkjoin</artifactId>
  <version>4.2.0</version>
</dependency>

Ivy

<dependency org="com.goldmansachs" name="gs-collections-api" rev="4.2.0" />
<dependency org="com.goldmansachs" name="gs-collections" rev="4.2.0" />
<dependency org="com.goldmansachs" name="gs-collections-testutils" rev="4.2.0" />
<dependency org="com.goldmansachs" name="gs-collections-forkjoin" rev="4.2.0"/>

New Functionality

SortedBag

SortedBag has all of the same properties as a Bag, and additionally maintains order by a Comparator or by the elements' natural order.

The main implementation is TreeBag which delegates to a TreeSortedMap to store its data.

MutableSortedBag<Integer> emptySortedBag = TreeBag.newBag();
MutableSortedBag<Integer> emptySortedBagWithComparator =
  TreeBag.newBag(Collections.reverseOrder());
MutableSortedBag<Integer> naturalOrder =
  TreeBag.newBagWith(1, 2, 3);
MutableSortedBag<Integer> reversedOrder =
  TreeBag.newBagWith(Collections.reverseOrder(), 4, 3, 3, 2, 2, 1);
MutableSortedBag<Integer> sortedBagFromFastList =
  TreeBag.newBag(FastList.newListWith(1, 2, 3));
MutableSortedBag<Integer> sortedBagFromFastListWithComparator =

TreeBag.newBag(Collections.reverseOrder(), FastList.newListWith(1, 2, 3));

BiMap

BiMap is a map that allows users to perform lookups from both directions. Both the keys and the values in a BiMap are unique.

The main implementation is HashBiMap.

inverse()

BiMap.inverse() returns a view where the position of the key type and value type are swapped.

MutableBiMap<Integer, String> biMap =
  HashBiMap.newWithKeysValues(1, "1", 2, "2", 3, "3");
MutableBiMap<String, Integer> inverse = biMap.inverse();
Assert.assertEquals("1", biMap.get(1));
Assert.assertEquals(Integer.valueOf(1), inverse.get("1"));
Assert.assertTrue(inverse.containsKey("3"));
Assert.assertEquals(Integer.valueOf(2), inverse.put("2", 4));

put()

MutableBiMap.put() behaves like Map.put() on a regular map, except it throws when a duplicate value is added.

MutableBiMap<Integer, String> biMap = HashBiMap.newMap();
biMap.put(1, "1"); // behaves like a regular put()
biMap.put(1, "1"); // no effect
biMap.put(2, "1"); // throws IllegalArgumentException

forcePut()

This behaves like MutableBiMap.put(), but it silently removes the map entry with the same value before putting the key-value pair in the map.

MutableBiMap<Integer, String> biMap = HashBiMap.newMap();
biMap.forcePut(1, "1"); // behaves like a regular put()
biMap.forcePut(1, "1"); // no effect
biMap.put(1, "2"); // replaces the [1,"1"] pair with [1, "2"]
biMap.forcePut(2, "2"); // removes the [1, "2"] pair before putting
Assert.assertFalse(biMap.containsKey(1));
Assert.assertEquals(HashBiMap.newWithKeysValues(2, "2"), biMap);

Optimize HashBag by delegating to ObjectIntHashMap

HashBag now delegates to ObjectIntHashMap<K> instead of a MutableMap<K, Counter>. This saves memory by eliminating the Counter wrapper objects.

Functions.chain()

The Functions.chain<primitive>() methods are similar to Functions.chain(), but they take a primitive function as the second argument. There are variants for all eight primitives:

  • chainBoolean()
  • chainByte()
  • chainChar()
  • chainDouble()
  • chainInt()
  • chainFloat()
  • chainLong()
  • chainShort()