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 (15-16).
1) Arrays
There are two different ways to work with arrays in the new framework. The Arrays class offers a series of static methods to perform common functions like sorting and searching, as shown in Chapter 13. Using the sorting and searching methods of the Arrays class isn't converting the array into something that is part of the Collections Framework. In order to convert an array into something that is part of the framework, you need to
use the asList() method of Arrays:
public static void main (String args[]) {
List list = Arrays.asList(args);
}
When the array is converted by the asList() method, the created List is updateable with the changes reflected in the original array. However, the list will not expand (or shrink) in size. If you try to add or remove an element to or from the returned list, an UnsupportedOperationException will be thrown. In the case of removal, the exception is thrown only if you try to remove something in the list. If the element isn't in the list,the exception will not be thrown.
2) Enumerations
The final conversion occurs in going from a Collection to an Enumeration. There is a helper method in the Collections class that does the conversion for us: Enumeration enumeration(Collection). Given any Collection, the enumeration() method will convert it into an Enumeration:
List l = Arrays.asList(args);
Enumeration enum = Collections.enumeration(l);
As with the toArray() method, if you'd like to convert a Map to an Enumeration, you need to work with the map's specific set of entries, keys, or values:
Enumeration enum = Collections.enumeration(map.entrySet());
3) Finding Additional Collections
The implementations found in the Collections Framework were created because they fill the general−purpose needs of most developers for collections. However, there are times when the general−purpose collections aren't enough. If you discover you need a special−purpose collection, you can either build it yourself or find someone else who already has. A good place to get started in finding additional collection implementations is the Java Collections Clearinghouse Web site at http://www.javacollections.org/. The maintainers keep a list of third−party implementations you can readily reuse, the licenses of which vary considerably, though many are open source. Before creating your own collection implementations, consider looking there first. You'll even
find special−purpose Comparator objects predefined, such as those for comparing file sizes. One word of caution about reusing other people's work: be sure to read the documentation for any special requirements. For instance, you'll find an implementation of a FastHashMap at http://www.crionics.com/projects/fhm/fhm.html. Designed purely for speed, it breaks under certain conditions, such as when you don't have unique hash codes. If your requirements happen to match needs that a predesigned collection fills, you can save yourself plenty of time. If, however, it doesn't or if you miss the warnings about the usage of a collection, you could be paddling for a long time. Be sure to bring a paddle.
4) Choosing the Right Collection
If you need to store primitive elements, you must use an array unless you want to place every item into a wrapper class like Integer or Float. Consider using a BitSet instead of an array of booleans.
Rarely, if ever, should you use an historical collection class like Hashtable or Vector. Sometimes you need to use a Properties object, which is a type of Hashtable. Unless explicitly called for—as when using the JavaMail API—the historical collection class should be avoided in favor of the newer
framework implementations.
Use a List if you need ordered access. Pick an ArrayList for indexed access. Definitely use a LinkedList when you need to add and remove elements from the beginning or middle of the list. If you only need ordered access through an Iterator, believe it or not, LinkedList is actually faster. Lists
are also good if your collection requires the storing of duplicates, triplicates, or more.
Sets are for storing unique items in a collection. If you need ordered access, use a TreeSet. Otherwise,use a HashSet. If you only need ordered access after all the elements have been added, consider creating the set with a HashSet, then copying all the elements into a TreeSet.
Use a Map if you need to store key−value pairs and the key isn't an integer. (Consider using an array or List for indexed access.) Like Set, you should always use the hashed version of the collection (HashMap) unless you need sorted access (TreeMap).
Rely on the Collections class to make collections and maps read−only and thread−safe.Use the WeakHashMap if you need to maintain weak references to keys stored in the map.
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).
Wednesday, 17 December 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment