You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2004/09/28 03:38:07 UTC

DO NOT REPLY [Bug 31444] New: - New CollectionUtils feature and tests: chooseRandomElement from collection

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=31444>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=31444

New CollectionUtils feature and tests: chooseRandomElement from collection

           Summary: New CollectionUtils feature and tests:
                    chooseRandomElement from collection
           Product: Commons
           Version: unspecified
          Platform: All
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Collections
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: ccesc@eonomine.com


I find myself needing to do this every so often, but there's no Java collections or Commons Collections 
functionality. CollectionUtils seemed like a good place for something like this to rest. I will attach the 
Java source for CollectionUtils.java from CVS around 6:30pm PDT complete with the new functions 
(there are two, plus a new static data member). Also, I will attach the source for TestCollectionUtils.java 
which includes tests for this new functionality. The new code is at the end of each class.

The function takes any collection, but is smart enough to use List's get() method if the collection is a 
list, resulting in constant time (or at least, much faster) behavior on classes which implement 
RandomAccess. Otherwise, the function simply picks a random index and linearly iterates to it, 
resulting in linear time complexity.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org