Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating Custom Generic Collections with J2SE 5.0 : Page 2

J2SE 5.0 introduced many additions to the collections API that you need to be aware of to properly implement generic custom collections that work seamlessly with multiple types and the new "for each" construct.


advertisement
Testing the Queue Class
The following class serves to test the "generic" queue.

package com.heatonresearch.examples.collections; public class TestQueue { public static void main(String args[]) { Queue<Integer> queue = new Queue<Integer>(); queue.push(1); queue.push(2); queue.push(3); try { System.out.println("Pop 1:" + queue.pop()); System.out.println("Pop 2:" + queue.pop()); System.out.println("Pop 3:" + queue.pop()); } catch (QueueException e) { e.printStackTrace(); } } }

The queue created in the preceding code accepts Integer objects only"

Queue<Integer> queue = new Queue<Integer>();

The test next adds three integers to the queue.



queue.push(1); queue.push(2); queue.push(3);

Notice that the numbers added to the queue are primitive types. Because of J2SE's autoboxing feature, these primitive int types are automatically converted into Integer objects.

Next, the test retrieves the objects using the pop method. The test catches the QueueException, in case the queue is empty. The results of popping three numbers from this queue will be.

1 2 3

Although shown here as a queue that accepts Integers, because of generics, this queue class will work with any Java object.

Creating a Peekable Stack Collection
Here's a more complex collection type that implements a stack that allows you to look ahead, or "peek" before actually removing an object. You can look ahead using either an iterator or J2SE 5.0's new "for each" construct.

The PeekableStack class is a first-in-last-out (FILO) stack that lets you iterate over the current stack contents. The implementation uses two classes. First, the PeekableStack class implements the actual stack. Second, the PeekableStackIterator class implements a "Java Standard" Iterator class that you can use to iterate over the stack. Listing 2 shows the PeekableStack class.

Notice that the PeekableStack class in Listing 2 implements the Iterable interface. This is necessary to support the new J2SE 5.0 "for-each" construct. The Iterable interface specifies that your collection supports the "iterator" method, which returns an iterator. Without this interface, your class is not compatible with the new "for-each" construct.

The peekable stack contains both push and pop methods, just like the queue. The push method is only slightly more complex than the queue. The push method adds the object onto the stack and increments the version.

The version variable allows the PeekableStackIterator class to ensure that no modifications have taken place. When the iterator is created the iterator keeps a copy of the current version number. If any changes to the stack occur through calls to the push method, the version numbers will not match; that mismatch causes the iterator to throw a ConcurrentModificationException.

The pop method is a little more complex. First it must determine the last element in the list, which it does by obtaining the size of the list and subtracting one.

int last = list.size() - 1;

If this results in a number less than zero, then the stack was empty, so the pop method returns null.

if (last < 0) return null;

If there is a "last element" in the stack, then retrieve it from the list. After retrieving the item successfully from the list you can remove it.

T result = list.get(last); list.remove(last);

Finally, return the object that was retrieved from the list.

return result;

To support "for each" iteration, the PeekableStack class's iterator method returns a "Java Standard" Iterator class that you can use to iterate over all the objects contained in the stack. The iterator method creates a new iterator and returns it.

PeekableStackIterator peekableStackIterator = new PeekableStackIterator(this, list);

As you can see, the iterator class accepts the current stack and the stack's list of items as constructor parameters. These values will be used by the PeekableStackIterator, which is covered in the next section.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap