Tuesday, 16 December 2008

Java Collections by John Zukowski - Part7

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 (13-14).

1) To check for equality of a subset of elements in one or both arrays, you'll need to make a copy of the array, most likely with the arraycopy() method of System.

2) Primitive Arrays

There are fourteen methods to sort arrays of primitive values in the Arrays class:

public static void sort(byte array[])
public static void sort(byte array[], int fromIndex, int toIndex)
public static void sort(char array[])
public static void sort(char array[], int fromIndex, int toIndex)
public static void sort(double array[])
public static void sort(double array[], int fromIndex, int toIndex)
public static void sort(float array[])
public static void sort(float array[], int fromIndex, int toIndex)
public static void sort(int array[])
public static void sort(int array[], int fromIndex, int toIndex)
public static void sort(long array[])
public static void sort(long array[], int fromIndex, int toIndex)
public static void sort(short array[])
public static void sort(short array[], int fromIndex, int toIndex)

3) Object Arrays

While sorting arrays of primitives is very straightforward, sorting arrays of objects can require a little more work. There are four sort() methods for sorting objects:

public static void sort(Object array[])
public static void sort(Object array[], int fromIndex, int toIndex)
public static void sort(Object array[], Comparator c)
public static void sort(Object array[], int fromIndex, int toIndex, Comparator c)

The first two methods work similar to the primitive array sorting methods. Pass in an array of objects and they'll be sorted. The objects in the array passed in, though, must implement the Comparable interface. If the objects don't implement the interface, when you try to sort the array, a ClassCastException will be thrown at runtime. To demonstrate, the following program will sort the elements passed in from the command line and
then print them out:


import java.util.*;
public class SortTest {
public static void main(String args[]) throws Exception {
Arrays.sort(args);
for (int i=0, n=args.length; i<n; i++) {
System.out.println(args[i]);
}
}
}




4) Implementing Optional Methods

There are three optional methods in the iterator for an abstract sequential list: add(), remove(), and set():

public void add(Object element)
public void remove()
public void set(Object element)

Which of the three methods you define depends on the operations your sequential list supports. If your sequential list can grow or shrink, you would implement add() or remove(), possibly both. If you want your list to be modifiable, you would implement set(). If your sequential list doesn't support an operation, the iterator will need to throw an UnsupportedOperationException.

Believe it or not, all the remaining methods of AbstractSequentialList rely on size() and iterator() for their behavior, mostly iterator(). If your backing support can provide some form of performance improvement, do override as iterating through all elements can be very costly.

5) Subclassing AbstractMap

Creating an abstract map is actually simpler than creating all the other collections. Here, you only have to define two constructors and one method entrySet().

In your custom implementation, you must provide a no−argument constructor and a copy constructor:

public MyMap()
public MyMap(Collection col)

If your custom map also implements SortedMap, then you must provide two additional constructors:

public MyMap(Comparator comp)
public MyMap(SortedMap Map)

As far as the method goes, you must implement the abstract entrySet() method:

public abstract Set entrySet()

Because a set knows its size, you don't have to override the size() method of AbstractMap.

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).

No comments: