Reasons to Write a Custom Collection Implementation in JAVA

by Mike Kremilen.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on java  

You are here: Categories » Computers and technology » JAVA

Many programmers will never need to implement their own Collections classes. Someday you might want to write your own implementation. It is fairly easy to do this with the aid of the abstract implementations provided by the Java platform. Before we discuss how to write an implementation, let's discuss why you might want to write one.

Reasons to Write an Implementation

The following list illustrates the sort of custom Collections you might want to implement. It is not intended to be exhaustive:

Persistent All of the built-in Collection implementations reside in main memory and vanish when the program exits. If you want a collection that will still be present the next time the program starts, you can implement it by building a veneer over an external database. Such a collection might be concurrently accessible by multiple programs.

Application-specific This is a very broad category. One example is an unmodifiable Map containing real-time telemetry data. The keys could represent locations, and the values could be read from sensors at these locations in response to the get operation.

High-performance, special-purpose Many data structures take advantage of restricted usage to offer better performance than is possible with general-purpose implementations. For instance, consider a List containing long runs of identical element values. Such lists, which occur frequently in text processing, can be runlength encodedruns can be represented as a single object containing the repeated element and the number of consecutive repetitions. This example is interesting because it trades off two aspects of performance: It requires less space but more time than an ArrayList.

High-performance, general-purpose The Java Collections Framework's designers tried to provide the best general-purpose implementations for each interface, but many, many data structures could have been used, and new ones are invented every day. Maybe you can come up with something faster!

Enhanced functionality Suppose you need an efficient bag implementation (also known as a multiset): a Collection that offers constant-time containment checks while allowing duplicate elements. It's reasonably straightforward to implement such a collection atop a HashMap.

Convenience You may want additional implementations that offer conveniences beyond those offered by the Java platform. For instance, you may frequently need List instances representing a contiguous range of Integers.

Adapter Suppose you are using a legacy API that has its own ad hoc collections' API. You can write an adapter implementation that permits these collections to operate in the Java Collections Framework. An adapter implementation is a thin veneer that wraps objects of one type and makes them behave like objects of another type by translating operations on the latter type into operations on the former.

Leave a comment or ask a question
Total comments: 0

JAVA Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Arithmetic Assignment Operators - The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the source variable twice when ass (more...)
Bitwise Operators - The following table shows the standard bitwise operators in Java and a description of them. (more...)
Invocation Chaining - Invocation chaining means that you are not limited to merely accessing one class/object member in a given statement with the . operator but may continue to access further members in a given stateme (more...)
Regular Expressions in Java - A regular expression is a code that is used to match a pattern in a given string and is new to Java 1.4. Regular expressions are made up of normal characters and metacharacters. Normal characters a (more...)
Character Escape Sequences - Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) character, followed by th (more...)
Conditional Statements - The ability to choose the path that your program takes, based on any given data, is the key to all functionality in programming. In order to create conditional statements, we must first learn a (more...)
Java Methods - Methods are used as the building blocks of your program, performing tasks that can be called again and again and using the same code to perform the task each time. The basic but fundamental parts o (more...)
Variable Scope - The scope of a variable is the area in which a variable belongs, specified by the area in which it is declared. The following example code contains two declared variables, one inside a code block a (more...)
What is a Java Package - A Java package is a collection of related classes that can be imported into your program to support your software. They also provide namespace management, as well as access protection. (more...)
Importing Java Packages - To use a package within a Java application or applet, we need to import it. We do this by means of the import keyword. So, for example, if we wish to include the I/O package, which is called java.i (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.