Presents your JAVA E-NEWSLETTER for May 5, 2003 <-------------------------------------------> GET THE MOST OUT OF THE COLLECTIONS CLASS The java.util.Collections class has lots of useful methods to make the programmer's job a little easier, but these methods are often underutilized. This tip will tour a sampling of some of those methods to explore their benefits. The javadoc gives the best description of the Collections class: "This class consists exclusively of static methods that operate on or return collections." Use the copy method to copy one java.util.List to another: Collections.copy(newList, sourceList); If you need to randomize a List, then call on the shuffle method: shuffle(list); This method will reorder the contents of the list using a "default source of randomness." If you'd like to provide your own source of randomness, then call its sibling method: shuffle(List list, Random random) If you need to create a Collection that can't be changed, then use the unmodifiableCollection(Collection c) method. This method has sibling methods that handle the more specific types of Set, List, and Map. In the case of Set and Map, you can even get the resulting objects sorted for you by using one of the sorted method variations: List newList = Collections.unmodifiableList(myList); If you have to work with legacy code that requires an Enumeration object, then you can take advantage of the enumeration method: Enumeration e = Collections.enumeration(collection); Whenever you need to replace a list of objects with a single object, use the fill(List list, Object obj) method. On the other hand, you may just need a list containing multiple references to the same object. In that case, use the nCopies(int n, Object o) method. When you need to find the location of one list within another, use the indexOfSubList(List source, List target) or lastIndexOfSubList(List source, List target) method. If the target list exists in the source list, then the methods return the index of the starting position of the contained list. If the target cannot be found in the source, then both methods return -1. If you need to reverse the order of a list, invoke the reverse method, which takes a list and reverses the order of the existing elements. As you can see, the collections class contains a large number of methods. Although you may never need some of them, you may employ others frequently. In any case, methods aren't the only thing the class contains. There are also three empty collection fields. EMPTY_LIST, EMPTY_MAP, and EMPTY_SET will help you avoid needless object creation when you need to return empty collections of data. For instance: public List getData(Criteria c) { ... if ( noData ) { return Collections.EMPTY_LIST; } // otherwise // gather and return data ... } You could have created a List type with no elements, but why create objects when you can avoid it? This tip shows a good sampling of the Collections class, but there are more methods available. For instance, there are methods for creating synchronized collections, for finding minimums and maximums, and more. Take a look for yourself--you just might find something useful. ----------------------------------------