commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Neil O'Toole <>
Subject [collections] added #unmodifiableXXXCopy and static #removeAll, #retainAll implementations
Date Sat, 11 Dec 2004 06:50:41 GMT
I've just committed a few utility/convenience methods as below:

List removeAll(Collection collection, Collection remove)
public static List retainAll(Collection collection, Collection retain)
public static List unmodifiableListCopy(Collection collection)

Same as ListUtils, except returning a Collection

public static Map unmodifiableMapCopy(Map map)

Comment: It's a very common thing to want to keep a record of a
collection's state. I've seen code peppered with the likes of:

List copy = ListUtils.unmodifiableList(new ArrayList(originalList));

Whilst that may not seem to be too much of an inconvenience, the
ArrayList constructor will automatically increase the size of the list
by 10%, and that memory is obviously never used. The supplied
implementation will create a new ArrayList of the correct size, call
#addAll to populate the copy and then return the unmodfiable-decorated
copy. Equivalent methods are supplied for MapUtils and CollectionUtils.

The #removeAll and #retainAll impls are for when you want to obtain the
product of these operations on a collection, but can't or don't want to
modify the collection. E.g. if the collection is unmodifiable. An
alternative is to make a copy of the collection, and then call
#removeAll, but this isn't as efficent as the implementation provided.

Any comments?


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message