GS Collections v4.2.0 Release Notes
Release Date: 2013-10-28 // over 10 years ago-
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 aBag
, and additionally maintains order by aComparator
or by the elements' natural order.The main implementation is
TreeBag
which delegates to aTreeSortedMap
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 likeMap.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 effectbiMap.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 effectbiMap.put(1, "2"); // replaces the [1,"1"] pair with [1, "2"]biMap.forcePut(2, "2"); // removes the [1, "2"] pair before puttingAssert.assertFalse(biMap.containsKey(1));Assert.assertEquals(HashBiMap.newWithKeysValues(2, "2"), biMap);
โก๏ธ Optimize HashBag by delegating to ObjectIntHashMap
HashBag
now delegates toObjectIntHashMap<K>
instead of aMutableMap<K, Counter>
. This saves memory by eliminating theCounter
wrapper objects.Functions.chain()
The
Functions.chain<primitive>()
methods are similar toFunctions.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()