Creating Custom Generic Collections with J2SE 5.0

Creating Custom Generic Collections with J2SE 5.0

y last article discussed how to use J2SE 5.0’s new collection features, allowing you to designate a specific type to be used with a collection. Additionally, the new “for each” construct lets you access collections without the need for an “iterator”. However, that was only half the story. This article shows you how to create collections that are compatible with the latest features of J2SE.

Creating Classes that Support Generics
First you must lean how to create a class that allows a “generic type”. This means that whenever your class is instantiated, you can also specify one or more Java types to be associated with that class. To illustrate this, consider this simple example class presented in Listing 1.

Notice how the class in Listing 1 is declared. It specifies three generics between the beginning angle brackets. Those generics are placeholders for real types. When you declare a class of this type, you can specify a class to take the place of ONE, TWO, and THREE. If you do not, then the class will use the default type of Object.

Here’s how to instantiate a class of type Example.

   Example example = new     Example();

The preceding code substitutes the specific Double, Integer, and String types for the “ONE,” “TWO,” and “THREE” placeholders in Listing 1. You can see that these variables have these types by the following three lines that set their values.

     example.setOne(1.5);     example.setTwo(2);     example.setThree("Three");

Now that you understand how to create a custom class that makes use of generics, it’s far simpler to create a custom collection class that makes use of generics.

Creating a Queue Class
A queue can be a very useful data structure. To understand the functions of a queue picture the line of people waiting for a ride at an amusement park. People enter the line at the back. They wait their turn, and eventually reach the front of the line. The order does not change.

This same principle can be applied to a queue class. There are two methods, named “push” and “pop”. You use the push method to place objects onto the queue, and the pop method to remove items from the queue. For example, if you use the push method to add three objects onto the queue, then calling pop three times will remove those three objects in the same order. This is the same as the amusement park line. If three people enter the line in a specific order, they will get access to the ride, in the same order.

The following code shows how to implement a Java queue that makes use of generics.

   package com.heatonresearch.examples.collections;      import java.util.*;      public class Queue {        private ArrayList list = new ArrayList();        public void push(T obj) {       list.add(obj);     }        public T pop() throws QueueException {       if (size() == 0)         throw new QueueException(             "Tried to pop something from the queue, " +             "when it was empty");       T result = list.get(0);       list.remove(0);       return result;     }        public boolean isEmpty() {       return list.isEmpty();     }        public int size() {       return list.size();     }        public void clear() {       list.clear();     }   }

The preceding code declares the queue class so that it accepts one generic type.

public class Queue

The generic type “T” is the class type that will be placed onto the queue. To store the items on a queue, the class creates an ArrayList that accepts types of “T” as well.

The push method is very simple. It accepts a single object, of the generic type “T”, and adds it to the ArrayList.

The pop method is slightly more complex. First, if you try to pop an object from the queue, and there are no objects on the queue, the class throws an exception of type QueueException. Here’s the QueueException class.

   package com.heatonresearch.examples.collections;   public class QueueException extends Exception {     public QueueException(String msg) {       super(msg);     }   }

Here’s the code that throws the QueueException:

   if (size() == 0)     throw new QueueException(       "Tried to pop something from the queue, " +        "when it was empty");

If the queue is not empty, the method retrieves the last element from the queue, stores it in a variable named result, then removes that item from the list. The following lines of code accomplish this.

     T result = list.get(0);     list.remove(0);     return result;

Note that the temporary variable is also of the generic type “T”. To ensure the greatest level of compatibility when this class is used with real Java types that will “stand in” for the generic type, it’s important to always use the generic type whenever you must access these variables.

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 queue = new Queue();       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 queue = new Queue();

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.

Creating a Peekable Stack Iterator
If the PeekableStack class is to be used with Java's new "for each" construct, then you must create a "Java Standard" iterator. The implementation for a PeekableStackIterator class is shown in Listing 3.

In Listing 3, the iterator does not actually change the value of the stack in any way; instead, the iterator keeps track of its current position in the element list and always returns the next element. Because this information is stored in the iteration class itself, it would be possible to have several iterators operating on the same stack.

The following program tests the Peekable stack.

   package com.heatonresearch.examples.collections;      import java.util.*;      public class TestPeekableStack {         public static void main(String args[]) {         PeekableStack stack = new          PeekableStack();         stack.push(1);         stack.push(2);         stack.push(3);            for (int i : stack) {            System.out.println(i);         }            System.out.println("Pop 1:" + stack.pop());         System.out.println("Pop 2:" + stack.pop());         System.out.println("Pop 3:" + stack.pop());      }   }

As you can see, three items are added to the stack. Then these three items are displayed using the new "for-each" construct.

   for( int i: stack)   {     System.out.println( i );   }

So, you've seen how to successfully implement a collection that supports the new J2SE conventions for both generics and the "for each" construct. As you can see, it's relatively easy to create collections that are compatible with the new constructs in J2SE 5.0 by taking advantage of generics and implementing the appropriate interfaces. You'll find that such collection classes will integrate seamlessly into J2SE 5.0.

devx-admin

devx-admin

Share the Post:
Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security.

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers

Geoengineering Methods

Scientists Dimming the Sun: It’s a Good Thing

Scientists at the University of Bern have been exploring geoengineering methods that could potentially slow down the melting of the West Antarctic ice sheet by reducing sunlight exposure. Among these

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists

AI Tool

Unleashing AI Power with Microsoft 365 Copilot

Microsoft has recently unveiled the initial list of Australian clients who will benefit from Microsoft 365 (M365) Copilot through the exclusive invitation-only global Early Access Program. Prominent organizations participating in

Microsoft Egnyte Collaboration

Microsoft and Egnyte Collaboration

Microsoft has revealed a collaboration with Egnyte, a prominent platform for content cooperation and governance, with the goal of improving real-time collaboration features within Microsoft 365 and Microsoft Teams. This

Best Laptops

Top Programming Laptops of 2023

In 2023, many developers prioritize finding the best laptop for programming, whether at home, in the workplace, or on the go. A high-performing, portable, and user-friendly laptop could significantly influence

Renaissance Gaming Magic

AI Unleashes A Gaming Renaissance

In recent times, artificial intelligence has achieved remarkable progress, with resources like ChatGPT becoming more sophisticated and readily available. Pietro Schirano, the design lead at Brex, has explored the capabilities

New Apple Watch

The New Apple Watch Ultra 2 is Awesome

Apple is making waves in the smartwatch market with the introduction of the highly anticipated Apple Watch Ultra 2. This revolutionary device promises exceptional performance, robust design, and a myriad

Truth Unveiling

Unveiling Truths in Bowen’s SMR Controversy

Tony Wood from the Grattan Institute has voiced his concerns over Climate and Energy Minister Chris Bowen’s critique of the Coalition’s support for small modular nuclear reactors (SMRs). Wood points

Avoiding Crisis

Racing to Defy Looming Financial Crisis

Chinese property developer Country Garden is facing a liquidity challenge as it approaches a deadline to pay $15 million in interest associated with an offshore bond. With a 30-day grace

Open-Source Development

Open-Source Software Development is King

The increasingly digital world has led to the emergence of open-source software as a critical factor in modern software development, with more than 70% of the infrastructure, products, and services

Home Savings

Sensational Savings on Smart Home Security

For a limited time only, Amazon is offering massive discounts on a variety of intelligent home devices, including products from its Ring security range. Running until October 2 or while