On Wed, 3 Sep 2003, Phil Steitz wrote: > Here is one code change that I would recommend: > > Currently, CollectionUtils.isProperSubCollection(a,b) returns > > CollectionUtils.isSubCollection(a,b) && > (!(CollectionUtils.isEqualCollection(a,b))); > > This is very inefficient, since it is equivalent to > > (a.size() < b.size()) && CollectionUtils.isSubCollection(a,b); > > There are currently no tests for this method, but the latter works for > the ones that I have coded. >
The latter approach assumes that the size() function returns the total number of elements in the collection (i.e., the "length" of the Iterator returned by collection.iterator()), whereas the former approach does not. For example, a Bag-ish implementation that returns the number of distinct elements for size(), but the duplicate elements in the iterator, would break the latter implementation. This assumption (that size() will return the number elements available in iterator()) is probably quite reasonable, and the latter implementation will be much more efficient when isProperSubCollection is going to return true, so I'd say go for it. (Though we may want to document the assumption.) Also, the latter approach doesn't quite work for extremely large collections. For example, if both a and b contain more than Integer.MAX_VALUE elements, this method may fail (since size() will still return Integer.MAX_VALUE in that case). This is probably more of an academic concern than an practical one, but again, may be a reasonable limitation to note. > Phil - Rod <http://radio.weblogs.com/0122027/> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
