1

I want to extend the ArrayList class to create a collection which doesn't hold duplicate element and use array internally to store. SetUniqueList stores unique element, but I want to add more functionality.

I am stuck in the method writeObject and readObject. This is the implementation in ArrayList:

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{
    int expectedModCount = modCount;
    s.defaultWriteObject();

    s.writeInt(elementData.length);

    for (int i=0; i<size; i++)
        s.writeObject(elementData[i]);

    if (modCount != expectedModCount) {
        throw new ConcurrentModificationException();
    }

}

private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
    s.defaultReadObject();

    int arrayLength = s.readInt();
    Object[] a = elementData = new Object[arrayLength];

    for (int i=0; i<size; i++)
        a[i] = s.readObject();
}

Clearly I cannot access private transient Object[] elementData;. I am puzzled that should I create class like ArrayList, not extending it, or there is any other artcitectural way to do this?

Update

I need to use subList so that changes in the sub list are reflected in this list.

3
  • 1
    Couldn't you use an existing implementation of the Set interface? Sets prevent duplicate elements. Commented Jun 8, 2013 at 14:13
  • Uh, you can just Set<X> set = new LinkedHashSet<X>(origList); List<X> list = new ArrayList<X>(set); // serialize list Commented Jun 8, 2013 at 14:16
  • @Funkytown yes I could use that but I need subList, so that changes in the sub list are reflected in this list. Commented Jun 8, 2013 at 14:18

1 Answer 1

1

I can think of a couple of ways to implement readObject and writeObject on a subclass of ArrayList:

  • Use reflection to call the private methods in ArrayList (Nasty!! But possibly justifiable.).

  • Use super.iterator() etctera to iterate the elements in your readObject method.


However, I think you would be better off turning your subclass of ArrayList into a wrapper class that delegates operations to a real ArrayList. Just make sure that the wrapper class is Serializable and the private variable containing the reference to the wrapped list is not marked as transient.)

An LinkedHashSet might be another option ... depending on how the collection is used.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I do not understand your first point about the reflection usage. Did you mean I need to call writeObject and readObject method of the super class ArrayList from writeObject and readObject method of my class? I can't do that. Isn't it?
@TapasBose - Yes. That's what I mean. You >>can<< do it if you use reflection, and the "dirty trick" that allows you to call another classes private methods: stackoverflow.com/questions/880365/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.