Commons-Collections provides a large number of classes to aid day to day programming. This document highlights some key features to get you started.
Commons-collections uses a design approach to synchronization similar
to the standard Java collections. The majority of the various implementations
of collections, maps and bags are not thread safe without additional
synchronization. The appropriate synchronizeXXX
method on Collections
is one way that these implementations can be synchronized for use in a
multithreaded application.
The class level javadocs should indicate whether a particular implementation is safe for multithreaded access without additional synchronization. Where there is no expicit indication that the implementation is thread safe then it should be assumed that synchronization is required. Please report the missing documentation to the commons development team.
A Utility class is provided for each major collection interface.
Thus, the Set
and SortedSet
interfaces are provided for by SetUtils.
These classes provide useful methods for working with that collection type.
The most methods are found on the two 'root' collection utility classes -
CollectionUtils
and MapUtils.
As all other collection interfaces extend Collection
or Map
these utilities can be used widely.
They include intersection, counting, iteration, functor and typecasting operations amongst others.
The utility classes also provide access to collection decorator classes in a way similar to the JDK Collections
class.
The JDK Map
interface always suffered from being difficult to iterate over.
API users are forced to either iterate over an EntrySet or over the KeySet.
Commons-Collections now provides a new interface - MapIterator
that allows simple iteration over maps.
A new interface is provided for maps that have an order but are not sorted - OrderedMap.
Two implementations are provided - LinkedMap
and ListOrderedMap
(a decorator).
This interface supports the map iterator, and also allows iteration both forwards and backwards through the map.
A new interface hierarchy has been added to support bidirectional maps - BidiMap.
These represent maps where the key can lookup the value and the value can lookup the key with equal ease.
Additional interfaces are provided for ordered and sorted bidirectional maps. Implementations are provided for each bidirectional map type.
A new interface hierarchy has been added to support queues and buffers - Buffer.
These represent collections that have a well defined removal order.
Implementations are provided for FIFO (queue), LIFO (stack) and Priority (removal in comparator order).
A new interface hierarchy has been added to support bags - Bag.
These represent collections where a certain number of copies of each element is held.
Implementations are provided for both unsorted and sorted Bags.