Couple of Days i started reading Java Collections by John Zukowski.
Nice book written by John Zukowski.
Few important quotations i wanted to share from the next 2 chapters (11-12).
1) Understanding Comparable
Assuming you'd like to know more about Comparable, here's how a properly written compareTo() method works:
public int compareTo(Object obj)
Elements must be mutually comparable.
You can only compare like elements. If you try to compare two elements that are not mutually comparable, a ClassCastException will be thrown. Quite simply, two elements are mutually comparable if they can be compared to one another. With respect to collection usage, this basically means don't try to add elements of type Date and File to a TreeSet—they aren't mutually comparable. In most cases, the two objects must be of the same type (or a subtype) to be mutually comparable. For instance, you can't compare a Double to a Float or any other numeric type.
The return value states the relative position to the natural ordering.
The compareTo() method can return one of three values. It will return a negative number if the current object comes before the object compared to. It will return a positive number if the current object comes after the object compared to. Finally, it will return zero if the two objects are equal. The magnitudes of the positive/negative numbers returned have no significance.
The natural ordering should be consistent with equals().
If two elements are equal as defined by the object's equals() method, then compareTo() should return zero. This rule is a should rule, not a must rule. If the class is not consistent, the javadoc associated to the class should include a note that reflects this, such as:
Note This class has a natural ordering that is inconsistent with equals.
Note If you implement Comparable for your custom class and it is not consistent with equals(), it won't work properly within a SortedSet or SortedMap.
Never call the method directly
The mechanisms within the Collections Framework call the compareTo() method for you when necessary. It is not your responsibility to call the method.
2) Creating Implementations
When creating concrete implementations of SortedSet, you should provide two additional constructors besides the no argument version and the copy constructor to accept a generic Collection. The first additional constructor would accept a custom Comparator, and the second, a SortedSet. In the case of the SortedSet constructor, the new set would use the same Comparator as the sorted set passed to it, creating a second set with the elements in the same order.
When creating new sorted sets from old sorted sets, you must be sure that the declaration type of the set passed into the constructor is correct. For instance, in the following example, the custom comparator will not be associated with the second set:
Set set1 = new TreeSet(new MyComparator());
Set set2 = new TreeSet(set1);
3) Singleton Collections
There exists a trio of methods for creating single−element collections, which act similar to the specialized methods for creating empty collections:
public static List singletonList(Object element)
public static Set singleton(Object element)
public static Map singletonMap(Object key, Object value)
4) Read−Only Collections
When working with collections, there are times when you need, or at least prefer, unmodifiable access to your collection instance. This may be because you are finished adding and removing elements from the collection but you still need to use the collection, or because you need to pass your collection to someone you don't necessarily know. Or perhaps you know them, but you don't want them to change anything. To ensure that your collections will not be modified, the Collections class provides a set of six methods to create read−only instances—one for each Collection, List, Map, Set, SortedMap, and SortedSet interface:
public static Collection unmodifiableCollection(Collection c)
public static List unmodifiableList(List l)
public static Map unmodifiableMap(Map m)
public static Set unmodifiableSet(Set s)
public static SortedMap unmodifiableSortedMap(SortedMap m)
public static SortedSet unmodifiableSortedSet(SortedSet s)
These methods work like the Decorator pattern by wrapping additional functionality around an underlying collection. This time, however, the decorated implementations remove functionality instead of adding capabilities. If you try to modify the collection directly or try to modify the collection through an acquired iterator, the underlying collection will remain unchanged and an UnsupportedOperationException will be thrown.
5) Reversing Lists
The reverse() method of Collections is used to reverse the order of the elements in a list:
public static void reverse(List list)
For instance, if you call reverse with a list of "Barney", "Carl", and "Lenny" as shown here
List barFlies = Arrays.asList(new String[] {"Barney", "Carl", "Lenny"});
Collections.reverse(barFlies);
System.out.println(barFlies);
6) Sorting Lists
The final two Collections methods named sort() have to do with reordering the elements of a List:
public static void sort(List list)
public static void sort(List list, Comparator comp)
The one−argument version of sort() requires that the elements of the list implement the Comparable interface.It relies on their natural ordering to order the elements in the list. If the elements don't implement the interface, when you try to sort the list, a ClassCastException will be thrown at runtime.
About the Author
John Zukowski has been involved with the Java platform since it was just called Java, 11 years and running, since 1995. He is actively working with SavaJe Technologies to finish up the JavaOne 2006 device of show: the Jasper S20 mobile phone. He currently writes a monthly column for Sun’s Core Java Technologies Tech Tips and Technology Fundamentals Newsletter. He has contributed content to numerous other sites, including jGuru, DevX, Intel, and JavaWorld. He has written many other popular titles on Java, including Java AWT Reference (O’Reilly), Mastering Java 2 (Sybex), Borlands’ JBuilder: No Experience Required (Sybex), Learn Java with JBuilder 6 (Apress), Java Collections (Apress), and The Definitive Guide to Swing (Apress).
Monday, 15 December 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment