| Java Doc By Examples | |
| Prev Class | Next Class | Frames | No Frames |
| Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Objectorg.apache.commons.collections.CollectionUtilspublic class CollectionUtilsextends ObjectCollection instances.
Field Summary | |
static Collection<E> |
|
Constructor Summary | |
| |
Method Summary | |
static void |
|
static void |
|
static void |
|
static boolean |
|
static int |
|
static Collection<E> |
|
static Collection<E> |
|
static Collection<E> |
|
static Collection<E> |
|
static boolean |
|
static int |
|
static Collection<E> |
|
static boolean |
|
static void |
|
static Object |
|
static void |
|
static Object | |
static Map<K,V> |
|
static Object |
|
static Object |
|
static Collection<E> |
|
static boolean |
|
static boolean |
|
static boolean |
|
static boolean |
|
static boolean |
|
static boolean |
|
static int |
|
static Collection<E> |
|
static Collection<E> |
|
static Collection<E> |
|
static void |
|
static Collection<E> |
|
static void |
|
static Collection<E> |
|
static void |
|
static int | |
static boolean |
|
static Collection<E> |
|
static Collection<E> |
|
static void |
|
static Collection<E> |
|
static Collection<E> |
|
static Collection<E> |
|
static Collection<E> |
|
Methods inherited from class java.lang.Object | |
clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait | |
public static final Collection<E> EMPTY_COLLECTION
An empty unmodifiable collection. The JDK provides empty Set and List implementations which could be used for this purpose. However they could be cast to Set or List which might be undesirable. This implementation only implements Collection.
public static void addAll(Collection<E> collection, Object[] elements)
Adds all elements in the array to the given collection.
- Parameters:
collection- the collection to add to, must not be nullelements- the array of elements to add, must not be null
- Throws:
NullPointerException- if the collection or array is null
1: import com.ejdoab.tcms.services.dto.ConferenceAbstractDTO; 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: _abstracts = new ArrayList(); 6: CollectionUtils.addAll(_abstracts, abstracts); 7: } 8: 9: public Object[] getElements() {
1: 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: _newsItems = new ArrayList(newsItems.length); 6: CollectionUtils.addAll(_newsItems, newsItems); 7: createModel(); 8: } 9:
1: 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.lang.ArrayUtils; 4: ... 5: ArrayList<TreeNode> children = new ArrayList<TreeNode>(); 6: CollectionUtils.addAll(children, node.getChildren()); 7: for (TreeNode child : children) { 8: TreeNode newChild = rewriteTree(child, rewriter); 9: if (newChild == null)
1: 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: if (this instanceof Group) 6: CollectionUtils.addAll(filters, ((Group)this).getFilters().iterator()); 7: if (this.getParent() != null) 8: filters.addAll(this.getParent().getInScopeFilterSet()); 9:
1: 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.oro.text.perl.Perl5Util; 4: ... 5: final String match = p5u.group(1); 6: CollectionUtils.addAll(attributes, StringUtils.split(match)); 7: } 8: return StringUtils.join(attributes.iterator(), " "); 9: }
public static void addAll(Collection<E> collection, Enumeration<E> enumeration)
Adds all elements in the enumeration to the given collection.
- Parameters:
collection- the collection to add to, must not be nullenumeration- the enumeration of elements to add, must not be null
- Throws:
NullPointerException- if the collection or enumeration is null
1: import com.ejdoab.tcms.services.dto.ConferenceAbstractDTO; 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: _abstracts = new ArrayList(); 6: CollectionUtils.addAll(_abstracts, abstracts); 7: } 8: 9: public Object[] getElements() {
1: 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: _newsItems = new ArrayList(newsItems.length); 6: CollectionUtils.addAll(_newsItems, newsItems); 7: createModel(); 8: } 9:
1: 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.lang.ArrayUtils; 4: ... 5: ArrayList<TreeNode> children = new ArrayList<TreeNode>(); 6: CollectionUtils.addAll(children, node.getChildren()); 7: for (TreeNode child : children) { 8: TreeNode newChild = rewriteTree(child, rewriter); 9: if (newChild == null)
1: 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: if (this instanceof Group) 6: CollectionUtils.addAll(filters, ((Group)this).getFilters().iterator()); 7: if (this.getParent() != null) 8: filters.addAll(this.getParent().getInScopeFilterSet()); 9:
1: 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.oro.text.perl.Perl5Util; 4: ... 5: final String match = p5u.group(1); 6: CollectionUtils.addAll(attributes, StringUtils.split(match)); 7: } 8: return StringUtils.join(attributes.iterator(), " "); 9: }
public static void addAll(Collection<E> collection, Iterator<E> iterator)
Adds all elements in the iteration to the given collection.
- Parameters:
collection- the collection to add to, must not be nulliterator- the iterator of elements to add, must not be null
- Throws:
NullPointerException- if the collection or iterator is null
1: import com.ejdoab.tcms.services.dto.ConferenceAbstractDTO; 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: _abstracts = new ArrayList(); 6: CollectionUtils.addAll(_abstracts, abstracts); 7: } 8: 9: public Object[] getElements() {
1: 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: _newsItems = new ArrayList(newsItems.length); 6: CollectionUtils.addAll(_newsItems, newsItems); 7: createModel(); 8: } 9:
1: 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.lang.ArrayUtils; 4: ... 5: ArrayList<TreeNode> children = new ArrayList<TreeNode>(); 6: CollectionUtils.addAll(children, node.getChildren()); 7: for (TreeNode child : children) { 8: TreeNode newChild = rewriteTree(child, rewriter); 9: if (newChild == null)
1: 2: import org.apache.commons.collections.CollectionUtils; 3: 4: ... 5: if (this instanceof Group) 6: CollectionUtils.addAll(filters, ((Group)this).getFilters().iterator()); 7: if (this.getParent() != null) 8: filters.addAll(this.getParent().getInScopeFilterSet()); 9:
1: 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.oro.text.perl.Perl5Util; 4: ... 5: final String match = p5u.group(1); 6: CollectionUtils.addAll(attributes, StringUtils.split(match)); 7: } 8: return StringUtils.join(attributes.iterator(), " "); 9: }
public static boolean addIgnoreNull(Collection<E> collection, Object object)
Adds an element to the collection unless the element is null.
- Parameters:
collection- the collection to add to, must not be nullobject- the object to add, if null it will not be added
- Returns:
- true if the collection changed
- Throws:
NullPointerException- if the collection is null
- Since:
- Commons Collections 3.2
public static int cardinality(Object obj, Collection<E> coll)
Returns the number of occurrences of obj in coll.
- Parameters:
obj- the object to find the cardinality ofcoll- the collection to search
- Returns:
- the the number of occurrences of obj in coll
public static Collection<E> collect(Collection<E> inputCollection, Transformer transformer)
Returns a new Collection consisting of the elements of inputCollection transformed by the given transformer. If the input transformer is null, the result is an empty list.
- Parameters:
inputCollection- the collection to get the input from, may not be nulltransformer- the transformer to use, may be null
- Returns:
- the transformed result (new list)
- Throws:
NullPointerException- if the input collection is null
1: import org.apache.commons.collections.Bag; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: { 6: return collection == null ? 0 : CollectionUtils.cardinality(item, collection); 7: } 8: ... 9: { 10: return CollectionUtils.intersection(first, second); 11: } 12: ... 13: { 14: return CollectionUtils.collect(collection, transformer);
public static Collection<E> collect(Collection<E> inputCollection, Transformer transformer, Collection<E> outputCollection)
Transforms all elements from inputCollection with the given transformer and adds them to the outputCollection. If the input collection or transformer is null, there is no change to the output collection.
- Parameters:
inputCollection- the collection to get the input from, may be nulltransformer- the transformer to use, may be nulloutputCollection- the collection to output into, may not be null
- Returns:
- the outputCollection with the transformed input added
- Throws:
NullPointerException- if the output collection is null
public static Collection<E> collect(Iterator<E> inputIterator, Transformer transformer)
Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection. If the input iterator or transformer is null, the result is an empty list.
- Parameters:
inputIterator- the iterator to get the input from, may be nulltransformer- the transformer to use, may be null
- Returns:
- the transformed result (new list)
1: import org.apache.commons.collections.Bag; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: { 6: return collection == null ? 0 : CollectionUtils.cardinality(item, collection); 7: } 8: ... 9: { 10: return CollectionUtils.intersection(first, second); 11: } 12: ... 13: { 14: return CollectionUtils.collect(collection, transformer);
public static Collection<E> collect(Iterator<E> inputIterator, Transformer transformer, Collection<E> outputCollection)
Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection. If the input iterator or transformer is null, there is no change to the output collection.
- Parameters:
inputIterator- the iterator to get the input from, may be nulltransformer- the transformer to use, may be nulloutputCollection- the collection to output into, may not be null
- Returns:
- the outputCollection with the transformed input added
- Throws:
NullPointerException- if the output collection is null
public static boolean containsAny(Collection<E> coll1, Collection<E> coll2)
Returnstrueiff at least one element is in both collections. In other words, this method returnstrueiff theintersection(Collection,Collection)of coll1 and coll2 is not empty.
- Parameters:
coll1- the first collection, must not be nullcoll2- the first collection, must not be null
- Returns:
trueiff the intersection of the collections is non-empty
- Since:
- 2.1
- See Also:
intersection(Collection,Collection)
public static int countMatches(Collection<E> inputCollection, Predicate predicate)
Counts the number of elements in the input collection that match the predicate. Anullcollection or predicate matches no elements.
- Parameters:
inputCollection- the collection to get the input from, may be nullpredicate- the predicate to use, may be null
- Returns:
- the number of matches for the predicate in the collection
public static Collection<E> disjunction(Collection<E> a, Collection<E> b)
Returns aCollectioncontaining the exclusive disjunction (symmetric difference) of the givenCollections. The cardinality of each element e in the returnedCollectionwill be equal to max(cardinality(e,a),cardinality(e,b)) - min(cardinality(e,a),cardinality(e,b)). This is equivalent tosubtract(union(a,b),intersection(a,b)) orunion(subtract(a,b),subtract(b,a)).
- Parameters:
a- the first collection, must not be nullb- the second collection, must not be null
- Returns:
- the symmetric difference of the two collections
1: import org.apache.commons.collections.Bag; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: { 6: return collection == null ? 0 : CollectionUtils.cardinality(item, collection); 7: } 8: ... 9: { 10: return CollectionUtils.intersection(first, second); 11: } 12: ... 13: { 14: return CollectionUtils.disjunction(first, second);
public static boolean exists(Collection<E> collection, Predicate predicate)
Answers true if a predicate is true for at least one element of a collection. Anullcollection or predicate returns false.
- Parameters:
collection- the collection to get the input from, may be nullpredicate- the predicate to use, may be null
- Returns:
- true if at least one element of the collection matches the predicate
1: import org.apache.commons.collections.Bag; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: { 6: return collection == null ? 0 : CollectionUtils.cardinality(item, collection); 7: } 8: ... 9: { 10: return CollectionUtils.intersection(first, second); 11: } 12: ... 13: { 14: return CollectionUtils.exists(collection, predicate);
public static void filter(Collection<E> collection, Predicate predicate)
Filter the collection by applying a Predicate to each element. If the predicate returns false, remove the element. If the input collection or predicate is null, there is no change made.
- Parameters:
collection- the collection to get the input from, may be nullpredicate- the predicate to use as a filter, may be null
public static Object find(Collection<E> collection, Predicate predicate)
Finds the first element in the given collection which matches the given predicate. If the input collection or predicate is null, or no element of the collection matches the predicate, null is returned.
- Parameters:
collection- the collection to search, may be nullpredicate- the predicate to use, may be null
- Returns:
- the first element of the collection which matches the predicate or null if none could be found
public static void forAllDo(Collection<E> collection, Closure closure)
Executes the given closure on each element in the collection. If the input collection or closure is null, there is no change made.
- Parameters:
collection- the collection to get the input from, may be nullclosure- the closure to perform, may be null
public static Object get(Object object, int index)
Returns theindex-th value inobject, throwingIndexOutOfBoundsExceptionif there is no such element orIllegalArgumentExceptionifobjectis not an instance of one of the supported types. The supported types, and associated semantics are:
- Map -- the value returned is the
Map.Entryin positionindexin the map'sentrySetiterator, if there is such an entry.- List -- this method is equivalent to the list's get method.
- Array -- the
index-th array entry is returned, if there is such an entry; otherwise anIndexOutOfBoundsExceptionis thrown.- Collection -- the value returned is the
index-th object returned by the collection's default iterator, if there is such an element.- Iterator or Enumeration -- the value returned is the
index-th object in the Iterator/Enumeration, if there is such an element. The Iterator/Enumeration is advanced toindex(or to the end, ifindexexceeds the number of entries) as a side effect of this method.
- Parameters:
object- the object to get a value fromindex- the index to get
- Returns:
- the object at the specified index
- Throws:
IndexOutOfBoundsException- if the index is invalidIllegalArgumentException- if the object type is invalid
public static Map<K,V> getCardinalityMap(Collection<E> coll)
Returns aMapmapping each unique element in the givenCollectionto anIntegerrepresenting the number of occurrences of that element in theCollection. Only those elements present in the collection will appear as keys in the map.
- Parameters:
coll- the collection to get the cardinality map for, must not be null
- Returns:
- the populated cardinality map
public static Object index(Object obj, int idx)
Deprecated. use
get(Object,int)instead. Will be removed in v4.0Given an Object, and an index, returns the nth value in the object.
- If obj is a Map, returns the nth value from the keySet iterator, unless the Map contains an Integer key with integer value = idx, in which case the corresponding map entry value is returned. If idx exceeds the number of entries in the map, an empty Iterator is returned.
- If obj is a List or an array, returns the nth value, throwing IndexOutOfBoundsException, ArrayIndexOutOfBoundsException, resp. if the nth value does not exist.
- If obj is an iterator, enumeration or Collection, returns the nth value from the iterator, returning an empty Iterator (resp. Enumeration) if the nth value does not exist.
- Returns the original obj if it is null or not a Collection or Iterator.
- Parameters:
obj- the object to get an index of, may be nullidx- the index to get
public static Object index(Object obj, Object index)
Deprecated. use
get(Object,int)instead. Will be removed in v4.0Given an Object, and a key (index), returns the value associated with that key in the Object. The following checks are made:
- If obj is a Map, use the index as a key to get a value. If no match continue.
- Check key is an Integer. If not, return the object passed in.
- If obj is a Map, get the nth value from the keySet iterator. If the Map has fewer than n entries, return an empty Iterator.
- If obj is a List or an array, get the nth value, throwing IndexOutOfBoundsException, ArrayIndexOutOfBoundsException, resp. if the nth value does not exist.
- If obj is an iterator, enumeration or Collection, get the nth value from the iterator, returning an empty Iterator (resp. Enumeration) if the nth value does not exist.
- Return the original obj.
- Parameters:
obj- the object to get an index ofindex- the index to get
- Returns:
- the object at the specified index
public static Collection<E> intersection(Collection<E> a, Collection<E> b)
Returns aCollectioncontaining the intersection of the givenCollections. The cardinality of each element in the returnedCollectionwill be equal to the minimum of the cardinality of that element in the two givenCollections.
- Parameters:
a- the first collection, must not be nullb- the second collection, must not be null
- Returns:
- the intersection of the two collections
1: import org.apache.commons.collections.Bag; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: { 6: return collection == null ? 0 : CollectionUtils.cardinality(item, collection); 7: } 8: ... 9: { 10: return CollectionUtils.intersection(first, second); 11: } 12: ... 13: { 14: return CollectionUtils.union(first, second);
public static boolean isEmpty(Collection<E> coll)
Null-safe check if the specified collection is empty. Null returns true.
- Parameters:
coll- the collection to check, may be null
- Returns:
- true if empty or null
- Since:
- Commons Collections 3.2
public static boolean isEqualCollection(Collection<E> a, Collection<E> b)
Returns true iff the givenCollections contain exactly the same elements with exactly the same cardinalities. That is, iff the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b.
- Parameters:
a- the first collection, must not be nullb- the second collection, must not be null
- Returns:
trueiff the collections contain the same elements with the same cardinalities.
1: import org.andromda.translation.ocl.syntax.VariableDeclaration; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: Collection operations = type.getOperations(); 6: this.contextElement = CollectionUtils.find(operations, new OperationFinder()); 7: if (this.contextElement == null) 8: ... 9: } 10: return CollectionUtils.isEqualCollection(expressionArgNames, facadeArgNames); 11: } 12: ... 13: Collection modelElements = this.model.getModelElements(); 14: CollectionUtils.filter(modelElements, new Predicate()
public static boolean isFull(Collection<E> coll)
Returns true if no more elements can be added to the Collection. This method uses theBoundedCollectioninterface to determine the full status. If the collection does not implement this interface then false is returned. The collection does not have to implement this interface directly. If the collection has been decorated using the decorators subpackage then these will be removed to access the BoundedCollection.
- Parameters:
coll- the collection to check
- Returns:
- true if the BoundedCollection is full
- Throws:
NullPointerException- if the collection is null
public static boolean isNotEmpty(Collection<E> coll)
Null-safe check if the specified collection is not empty. Null returns false.
- Parameters:
coll- the collection to check, may be null
- Returns:
- true if non-null and non-empty
- Since:
- Commons Collections 3.2
public static boolean isProperSubCollection(Collection<E> a, Collection<E> b)
Returns true iff a is a proper sub-collection of b, that is, iff the cardinality of e in a is less than or equal to the cardinality of e in b, for each element e in a, and there is at least one element f such that the cardinality of f in b is strictly greater than the cardinality of f in a. The implementation assumes
a.size()andb.size()represent the total cardinality of a and b, resp.a.size() <32Integer.MAXVALUE
- Parameters:
a- the first (sub?) collection, must not be nullb- the second (super?) collection, must not be null
- Returns:
trueiff a is a proper sub-collection of b
public static boolean isSubCollection(Collection<E> a, Collection<E> b)
Returns true iff a is a sub-collection of b, that is, iff the cardinality of e in a is less than or equal to the cardinality of e in b, for each element e in a.
- Parameters:
a- the first (sub?) collection, must not be nullb- the second (super?) collection, must not be null
- Returns:
trueiff a is a sub-collection of b
public static int maxSize(Collection<E> coll)
Get the maximum number of elements that the Collection can contain. This method uses theBoundedCollectioninterface to determine the maximum size. If the collection does not implement this interface then -1 is returned. The collection does not have to implement this interface directly. If the collection has been decorated using the decorators subpackage then these will be removed to access the BoundedCollection.
- Parameters:
coll- the collection to check
- Returns:
- the maximum size of the BoundedCollection, -1 if no maximum size
- Throws:
NullPointerException- if the collection is null
public static Collection<E> predicatedCollection(Collection<E> collection, Predicate predicate)
Returns a predicated (validating) collection backed by the given collection. Only objects that pass the test in the given predicate can be added to the collection. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original collection after invoking this method, as it is a backdoor for adding invalid objects.
- Parameters:
collection- the collection to predicate, must not be nullpredicate- the predicate for the collection, must not be null
- Returns:
- a predicated collection backed by the given collection
- Throws:
IllegalArgumentException- if the Collection is null
public static Collection<E> removeAll(Collection<E> collection, Collection<E> remove)
Removes the elements inremovefromcollection. That is, this method returns a collection containing all the elements incthat are not inremove. The cardinality of an elementein the returned collection is the same as the cardinality ofeincollectionunlessremovecontainse, in which case the cardinality is zero. This method is useful if you do not wish to modify the collectioncand thus cannot callcollection.removeAll(remove);.
- Parameters:
collection- the collection from which items are removed (in the returned collection)remove- the items to be removed from the returnedcollection
- Returns:
- a
Collectioncontaining all the elements ofcollectionexcept any elements that also occur inremove.
- Throws:
NullPointerException- if either parameter is null
- Since:
- Commons Collections 3.2
public static Collection<E> retainAll(Collection<E> collection, Collection<E> retain)
Returns a collection containing all the elements incollectionthat are also inretain. The cardinality of an elementein the returned collection is the same as the cardinality ofeincollectionunlessretaindoes not containe, in which case the cardinality is zero. This method is useful if you do not wish to modify the collectioncand thus cannot callc.retainAll(retain);.
- Parameters:
collection- the collection whose contents are the target of the #retailAll operationretain- the collection containing the elements to be retained in the returned collection
- Returns:
- a
Collectioncontaining all the elements ofcollectionthat occur at least once inretain.
- Throws:
NullPointerException- if either parameter is null
- Since:
- Commons Collections 3.2
public static void reverseArray(Object[] array)
Reverses the order of the given array.
- Parameters:
array- the array to reverse
public static Collection<E> select(Collection<E> inputCollection, Predicate predicate)
Selects all elements from input collection which match the given predicate into an output collection. Anullpredicate matches no elements.
- Parameters:
inputCollection- the collection to get the input from, may not be nullpredicate- the predicate to use, may be null
- Returns:
- the elements matching the predicate (new list)
- Throws:
NullPointerException- if the input collection is null
1: import org.apache.commons.collections.Bag; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: { 6: return collection == null ? 0 : CollectionUtils.cardinality(item, collection); 7: } 8: ... 9: { 10: return CollectionUtils.intersection(first, second); 11: } 12: ... 13: { 14: return CollectionUtils.select(collection, predicate);
public static void select(Collection<E> inputCollection, Predicate predicate, Collection<E> outputCollection)
Selects all elements from input collection which match the given predicate and adds them to outputCollection. If the input collection or predicate is null, there is no change to the output collection.
- Parameters:
inputCollection- the collection to get the input from, may be nullpredicate- the predicate to use, may be nulloutputCollection- the collection to output into, may not be null
public static Collection<E> selectRejected(Collection<E> inputCollection, Predicate predicate)
Selects all elements from inputCollection which don't match the given predicate into an output collection. If the input predicate isnull, the result is an empty list.
- Parameters:
inputCollection- the collection to get the input from, may not be nullpredicate- the predicate to use, may be null
- Returns:
- the elements not matching the predicate (new list)
- Throws:
NullPointerException- if the input collection is null
1: import org.apache.commons.collections.Bag; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: { 6: return collection == null ? 0 : CollectionUtils.cardinality(item, collection); 7: } 8: ... 9: { 10: return CollectionUtils.intersection(first, second); 11: } 12: ... 13: { 14: return CollectionUtils.selectRejected(collection, predicate);
public static void selectRejected(Collection<E> inputCollection, Predicate predicate, Collection<E> outputCollection)
Selects all elements from inputCollection which don't match the given predicate and adds them to outputCollection. If the input predicate isnull, no elements are added tooutputCollection.
- Parameters:
inputCollection- the collection to get the input from, may be nullpredicate- the predicate to use, may be nulloutputCollection- the collection to output into, may not be null
public static int size(Object object)
Gets the size of the collection/iterator specified. This method can handles objects as follows
- Collection - the collection size
- Map - the map size
- Array - the array size
- Iterator - the number of elements remaining in the iterator
- Enumeration - the number of elements remaining in the enumeration
- Parameters:
object- the object to get the size of
- Returns:
- the size of the specified collection
- Throws:
IllegalArgumentException- thrown if object is not recognised or null
- Since:
- Commons Collections 3.1
public static boolean sizeIsEmpty(Object object)
Checks if the specified collection/array/iterator is empty. This method can handles objects as followsNote: This method is named to avoid clashing with
- Collection - via collection isEmpty
- Map - via map isEmpty
- Array - using array size
- Iterator - via hasNext
- Enumeration - via hasMoreElements
isEmpty(Collection).
- Parameters:
object- the object to get the size of, not null
- Returns:
- true if empty
- Throws:
IllegalArgumentException- thrown if object is not recognised or null
- Since:
- Commons Collections 3.2
public static Collection<E> subtract(Collection<E> a, Collection<E> b)
Returns a newCollectioncontaining a - b. The cardinality of each element e in the returnedCollectionwill be the cardinality of e in a minus the cardinality of e in b, or zero, whichever is greater.
- Parameters:
a- the collection to subtract from, must not be nullb- the collection to subtract, must not be null
- Returns:
- a new collection with the results
- See Also:
Collection.removeAll(Collection>)
public static Collection<E> synchronizedCollection(Collection<E> collection)
Returns a synchronized collection backed by the given collection. You must manually synchronize on the returned buffer's iterator to avoid non-deterministic behavior:Collection c = CollectionUtils.synchronizedCollection(myCollection); synchronized (c) { Iterator i = c.iterator(); while (i.hasNext()) { process (i.next()); } }This method uses the implementation in the decorators subpackage.
- Parameters:
collection- the collection to synchronize, must not be null
- Returns:
- a synchronized collection backed by the given collection
- Throws:
IllegalArgumentException- if the collection is null
public static void transform(Collection<E> collection, Transformer transformer)
Transform the collection by applying a Transformer to each element. If the input collection or transformer is null, there is no change made. This routine is best for Lists, for which set() is used to do the transformations "in place." For other Collections, clear() and addAll() are used to replace elements. If the input collection controls its input, such as a Set, and the Transformer creates duplicates (or are otherwise invalid), the collection may reduce in size due to calling this method.
- Parameters:
collection- the collection to get the input from, may be nulltransformer- the transformer to perform, may be null
1: { 2: org.apache.commons.collections.CollectionUtils.transform(entities, CHILDENTITYVO_TRANSFORMER); 3: } 4: ... 5: } 6: org.apache.commons.collections.CollectionUtils.transform(instances, ChildEntityVOToEntityTransformer); 7: } 8: }
1: { 2: org.apache.commons.collections.CollectionUtils.transform(entities, ATTRIBUTETWO_TRANSFORMER); 3: } 4: ... 5: } 6: org.apache.commons.collections.CollectionUtils.transform(instances, AttributeTwoToEntityTransformer); 7: } 8: }
1: { 2: org.apache.commons.collections.CollectionUtils.transform(entities, SUPERENTITYVO2_TRANSFORMER); 3: } 4: ... 5: } 6: org.apache.commons.collections.CollectionUtils.transform(instances, SuperEntityVO2ToEntityTransformer); 7: } 8: ... 9: { 10: org.apache.commons.collections.CollectionUtils.transform(entities, SUPERENTITYVO1_TRANSFORMER); 11: } 12: ... 13: } 14: org.apache.commons.collections.CollectionUtils.transform(instances, SuperEntityVO1ToEntityTransformer);
1: { 2: org.apache.commons.collections.CollectionUtils.transform(entities, VALUEOBJECT_TRANSFORMER); 3: } 4: ... 5: } 6: org.apache.commons.collections.CollectionUtils.transform(instances, ValueObjectToEntityTransformer); 7: } 8: }
1: { 2: org.apache.commons.collections.CollectionUtils.transform(entities, SUBENTITYVO2_TRANSFORMER); 3: } 4: ... 5: } 6: org.apache.commons.collections.CollectionUtils.transform(instances, SubEntityVO2ToEntityTransformer); 7: } 8: ... 9: { 10: org.apache.commons.collections.CollectionUtils.transform(entities, SUBENTITYVO1_TRANSFORMER); 11: } 12: ... 13: } 14: org.apache.commons.collections.CollectionUtils.transform(instances, SubEntityVO1ToEntityTransformer);
public static Collection<E> transformedCollection(Collection<E> collection, Transformer transformer)
Returns a transformed bag backed by the given collection. Each object is passed through the transformer as it is added to the Collection. It is important not to use the original collection after invoking this method, as it is a backdoor for adding untransformed objects.
- Parameters:
collection- the collection to predicate, must not be nulltransformer- the transformer for the collection, must not be null
- Returns:
- a transformed collection backed by the given collection
- Throws:
IllegalArgumentException- if the Collection or Transformer is null
public static Collection<E> typedCollection(Collection<E> collection, Class<T> type)
Returns a typed collection backed by the given collection. Only objects of the specified type can be added to the collection.
- Parameters:
collection- the collection to limit to a specific type, must not be nulltype- the type of objects which may be added to the collection
- Returns:
- a typed collection backed by the specified collection
public static Collection<E> union(Collection<E> a, Collection<E> b)
Returns aCollectioncontaining the union of the givenCollections. The cardinality of each element in the returnedCollectionwill be equal to the maximum of the cardinality of that element in the two givenCollections.
- Parameters:
a- the first collection, must not be nullb- the second collection, must not be null
- Returns:
- the union of the two collections
- See Also:
Collection.addAll(E>)
1: import org.apache.commons.collections.Bag; 2: import org.apache.commons.collections.CollectionUtils; 3: import org.apache.commons.collections.Predicate; 4: ... 5: { 6: return collection == null ? 0 : CollectionUtils.cardinality(item, collection); 7: } 8: ... 9: { 10: return CollectionUtils.intersection(first, second); 11: } 12: ... 13: { 14: return CollectionUtils.union(first, second);
public static Collection<E> unmodifiableCollection(Collection<E> collection)
Returns an unmodifiable collection backed by the given collection. This method uses the implementation in the decorators subpackage.
- Parameters:
collection- the collection to make unmodifiable, must not be null
- Returns:
- an unmodifiable collection backed by the given collection
- Throws:
IllegalArgumentException- if the collection is null