Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Custom Collection Implementations 
 

So far you have learnt about the Java built-in Collection Interfaces implementations.

 

Custom Collection Implementations

                         

So far you have learnt about the Java  built-in Collection Interfaces implementations. Apart from these, some times programmer need to implement their own collections classes.  The Java platform allows you to write your own implementation of a core collection interface. It is easy to write your own  implementation with the help of abstract implementations

Lets discuss, why we should make a custom implementation.

  • Persistent: All of the built-in Collection implementations reside in main memory and appears when the program exits. If you want a collection to be available for the next time when the program starts, you can implement a collection that is concurrently accessible by multiple programs.
  • High-performance, special-purpose: Many data structures take advantage of restricted usage to offer better performance that is possible with general-purpose implementations. For instance, consider a List containing long runs of identical element values. The runs can be represented as a single object containing the repeated element. This example is interesting because it deals with two aspects of performance: It requires less space but more time than an ArrayList.
  • High-performance, general-purpose: The Java Collections Framework's designers have tried to provide the best general-purpose implementations for each interface, but many, new ones are invented every day for the High performance of an application.
  • Convenience: Some times you want additional implementations that offers conveniences than those offered by the Java platform. For instance, you may need a List to represent a contiguous range of Integers.

To write your own custom implementation is not difficult. Java supports the abstract implementations to implement your own collection. Lets see the way of writing the custom collection implementation.

import java.util.*;

  class MyClass {
    public static List myList(Object[] a) {
       return new ArrayList(a);
    }
  }
    class ArrayList extends AbstractList
               implements java.io.Serializable {
    
  private Object[] x;

  ArrayList(Object[] array) {
      x = array;
  }
  public Object get(int index) {
      return x[index];
  }
  public Object set(int index, Object element) {
      Object oldVal = x[index];
      x[index= element;
      return oldVal;
  }
  public int size() {
      return x.length;
  }
    }
  public class CustomImpl{
   public static void main(String[] args) {
        try{
      String s[]={"My""Custom""Implementation"};
      Object o;
      int i=0;
      MyClass a= new MyClass();
      List lst=a.myList(s);
      System.out.println("The list is: "+lst);
      ArrayList al=new ArrayList(s);
      o=al.get(1);
      System.out.println("The retrieved element is: "+o);
      String s1="Collection";
      o=al.set(2,s1);
      System.out.println("The set element in place of Implementation is: "+s1);
      System.out.println("Now the new list is: "+lst);
      i=al.size();
      System.out.println("The size of the array list is: "+i);
      }
      catch(Exception e){}
    }
    }


Output of the Program:

C:\nisha>javac CustomImpl.java

C:\nisha>java CustomImpl
The list is: [My, Custom, Implementation]
The retrieved element is: Custom
The set element in place of Implementation is: Collection
Now the new list is: [My, Custom, Collection]
The size of the array list is: 3

C:\nisha>

Description of the Program:

In the given program, a custom implementation of Arrays.myList is defined which calls the constructor of ArrayList class and pass the object to it. The get( ) and the set( ) methods of the ArrayList class retrieve and set an element to the specified position of the List.

Download this Program

                         

» View all related tutorials
Related Tags: c algorithm api ide collections orm ant data form interface framework software io multiple ip order vi collection tag int

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.