Apache Commons Collections
Apache
Commons Collections (version 4 and later) extends Java's collection classes with
new interfaces, data structures, and utilities. I've specified the version number
since this was when the library was updated to support generics and
lambdas, which removed its main shortcoming compared to its competitor,
Google Guava.
Apache Collections is currently at v4.4.
To use Apache Collections (or Google Guava) effectively, it's important
to have a reasonable understanding of Java's
built-in
collections, which I've summarized in the diagram below:
There are four groups: lists, queues, sets, and maps
(located in a separate hierarchy). Missing from the diagram
are the many classes and interfaces related to concurrency and
serializability.
Apache Collections adds many data structures, including
bags, bidirectional maps, and maps with multiple keys or values. It also
makes wide use of lambda functions to control how structures are
built through object transformation, filtering, closures, and
composition.
Two places where I feel Google Guava may still have an edge is its
support for
bloom filters and its
Table data structure.
Bloom filters
aren't that common, but
they've come up during my teaching of algorithms. Rather than import all
of Google Guava, I've added Magnus Skjegstad's small, standalone
Java-Bloomfilter
library.
In a similar way, instead of using Guava's Table, I've found it
easier to add David Tesler's
MultiKeyMap library.
It offers a multi-key map similar to Apache's but
a value can be accessed using combinations of keys (see
PartialMultiKeyMapTester.java
for examples).
This means that I'm using three JAR files:
commons-collections4-4.4.jar, bloom-filter.jar
and multikeymapjava-0.7.0.jar. They can be downloaded from their
websites, or as a zipped file from here (see the "Downloads"
section below).
My examples are divided into five groups. You can obtain each file
separately, but it may be easier to download all the code as a single
zipped file (see the "Downloads" section).
1. Maps
- BidiMapTester.java:
a bi-directional map where the key-to-value relation
can be inverted, so values can be used as keys.
- MultiKeyMapTester.java:
a map where multiple keys can be associated with a value.
- PartialMultiKeyMapTester.java:
David Tesler's multiple keys map with the ability to access
values by combining keys.
- MultiValuedMapTester.java:
maps a key to a collection of values. (It's the successor to
MultiMap, which was deprecated in Collections v4.1.)
- LRUMapTester.java:
a limited-size cache where the least recently used (LRU) elements
are deleted to make room for new elements when the cache is full.
- OrderedMapTester.java:
A map that maintains its addition order, and allows both
forward and backward iteration over its data.
- ExpiringMapTester:
PassiveExpiringMap decorates a map to delete
entries once an expiration time is reached.
- TransformedMapTester:
TransformedMap decorates a map so it can
transform values before they are added to it.
- SplitMapTester:
TransformedSplitMap is a more powerful form of
TransformedMap which lets the key and value types
both be changed.
- PredicateMapTester.java:
a PredicatedMap decorates a map so it can validate
that an addition matches a specified predicate.
- MapUtilsTester.java:
illustrates a range of functions from MapUtils, including
invertMap, fixedSizeMap, predicatedMap,
populateMap, and lazyMap.
2. Lists
- ListsTester.java: a potpourri of
lists, including
CursorableLinkedList, FixedSizeList,
GrowthList, LazyList, PredicatedList,
SetUniqueList, and TransformedList.
- ListUtilsTester.java:
examples of set-like operations (e.g. union, intersection, sum, subtraction),
selection, partitioning, and permutations.
- CollectionUtilsTester.java:
mostly list examples of collation (list merging)
collecting, filtering, and more ways to perform set operations.
- IteratorsTester.java:
iteration-based examples of filtering, chaining, zipping,
transforming, counting, search, and fluents.
- Employee.java:
used by several of the examples.
3. Bags and Sets
- BagTester.java:
a bag stores multiple items along with their repetition count.
- MultiSetTester.java:
a MultiSet allows for multiple occurrences of each element,
keeping track of their count.
- SetUtilsTester.java:
set operations which return new sets, and examples of
predicatedSet, transformedSet, and
orderedSet.
4. Other Data Structures
- CircularQueueTester.java:
a fixed-size, non-blocking queue, so that when you add an element
to a full queue, the oldest element is removed to make room
for the new one.
- QueueTester.java:
various data structures, including
CircularFifoQueue, UnmodifiableQueue,
PredicatedQueue, and TransformedQueue.
- TrieTester.java: a
PATRICIA trie is a prefix search tree which
stores information in nodes other than its leaves.
- BloomTester.java:
utilizes Magnus Skjegstad's BloomFilter class.
- EditScriptTester.java:
examples of edit sequences, utilizing
CollectVisitor.java.
5. Utilities
Other Downloads and Links
Dr. Andrew Davison
E-mail: ad@coe.psu.ac.th
Back to the third-party libraries page