Collections Framework: Custom Collection Implementations
//How to write a custom implementation:
public static <T> List<T> asList(T[] a) {
return new MyArrayList<T>(a);
}
private static class MyArrayList<T> extends AbstractList<T> {
private final T[] a;
MyArrayList(T[] array) {
a = array;
}
public T get(int index) {
return a[index];
}
public T set(int index, T element) {
T oldValue = a[index];
a[index] = element;
return oldValue;
}
public int size() {
return a.length;
}
} The following list illustrates the sort of custom Collections you might want to implement: persistent, application-specific, high-performance+special-purpose, high-performance+general-purpose, enhanced functionality, convenience, adapter.
The process of writing a custom implementation follows: 1 - choose the appropriate abstract implementation class from the preceding list; 2 - provide implementations for all the abstract methods of the class; 3 - test and, if necessary, debug the implementation;4 - if you are concerned about performance, read the API documentation of the abstract implementation class for all the methods whose implementations you're inheriting.
Semantic portal