User guideCommons-Collections provides a large number of classes to aid day to day programming. This document highlights some key features to get you started.
Note On Synchronization
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 The class level javadocs should indicate whether a particular implementation is safe for multithreaded access without additional synchronization. Where there is no explicit 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. Utilities
A Utility class is provided for each major collection interface.
Thus, the
The most methods are found on the two 'root' collection utility classes -
MapsMap Iteration
The JDK IterableMap map = new HashedMap(); MapIterator it = map.mapIterator(); while (it.hasNext()) { Object key = it.next(); Object value = it.getValue(); it.setValue(newValue); } Ordered Maps
A new interface is provided for maps that have an order but are not sorted - OrderedMap map = new LinkedMap(); map.put("FIVE", "5"); map.put("SIX", "6"); map.put("SEVEN", "7"); map.firstKey(); // returns "FIVE" map.nextKey("FIVE"); // returns "SIX" map.nextKey("SIX"); // returns "SEVEN" Bidirectional Maps
A new interface hierarchy has been added to support bidirectional maps - BidiMap bidi = new TreeBidiMap(); bidi.put("SIX", "6"); bidi.get("SIX"); // returns "6" bidi.getKey("6"); // returns "SIX" bidi.removeValue("6"); // removes the mapping BidiMap inverse = bidi.inverseBidiMap(); // returns a map with keys and values swapped Additional interfaces are provided for ordered and sorted bidirectional maps. Implementations are provided for each bidirectional map type. Bags
A new interface hierarchy has been added to support bags - Bag bag = new HashBag(); bag.add("ONE", 6); // add 6 copies of "ONE" bag.remove("ONE", 2); // removes 2 copies of "ONE" bag.getCount("ONE"); // returns 4, the number of copies in the bag (6 - 2) Implementations are provided for both unsorted and sorted Bags. |