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.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • 1
    Couldn't you use an existing implementation of the Set interface? Sets prevent duplicate elements. – Funkytown Jun 08 '13 at 14:13
  • Uh, you can just `Set set = new LinkedHashSet(origList); List list = new ArrayList(set); // serialize list` – fge Jun 08 '13 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. – Tapas Bose Jun 08 '13 at 14:18

1 Answers1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 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? – Tapas Bose Jun 08 '13 at 14:31
  • @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: http://stackoverflow.com/questions/880365/any-way-to-invoke-a-private-method – Stephen C Jun 08 '13 at 23:21