|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 5
Many of the additional features such as sorting and synchronization are encapsulated in the Collections and Arrays classes. These classes, which will appear throughout the following discussion, provide static methods for acting on collections.
Sorting a collection
We'll begin by exploring sorting. Two of the concrete implementations in the Java Collections Framework provide easy means
to maintain a sorted collection: TreeSet and TreeMap. In fact, these two classes implement the SortedSet and SortedMap interfaces, which are similar to their unsorted counterparts except that they provide methods to access first and last elements
and portions of the sorted collections.
There are two basic techniques for maintaining a sorted collection. The first uses one of the sorted collection classes and
provides the collection with an object that implements a comparison via the Comparator interface. For example, going back to our first code example, we can sort our collection by creating a StringComparator and adding it to the end of the code, as shown here:
// This class sorts two String objects.
class StringComparator implements Comparator {
public int compare( Object object1, Object object2 ) {
return ((String) object1).compareTo( (String) object2 );
}
}
Next, we need to change the collection from a HashSet (unsorted) to a HashMap (sorted with our StringComparator by using the following constructor:
TreeSet collection = new TreeSet( new StringComparator() );
Rerun the example and you should see that the iteration is performed in sorted order. Because the collection is ordered, you
should now be able to find the min and the max elements using the static class Collections.
The second technique is to implement natural ordering of a class by making the class implement the Comparable interface. This technique adds a single compareTo method to a class, which then returns 0 for equal objects, less than 0 if the first parameter is less than the second, or
greater than 0 of the first parameter is greater than the second. In Java 1.2, the String class (but not StringBuffer) implements the Comparable interface. Any comparable object can be placed in a sorted collection, and the collection order is maintained automatically
by the collection.
You can also sort Lists by handing them to the Collections class. One static sort method takes a single List parameter that specifies a naturally ordered class (one that implements the Comparable interface). A second static sort method takes a Comparator object for other classes that do not implement the Comparable interface.
Unmodifiable collections
The Collections class provides many static factory methods (like Collection.unmodifiableCollection and Collection.unmodifiableSet) for providing unmodifiable or immutable collections. In fact, there is one method for each of the basic collection interfaces.
These methods are extremely useful to ensure that no one modifies your collection. For instance, if you want to allow others
to see your list but not change it, you may implement a method that returns an unmodifiable view of your collection. Here's
an example: