# Changes in Collections Classes to Support Serialization

To the best of my knowledge, only the following changes are needed to support Java-1.1 Serialization, as implemented in the current alpha RMI release.

1. Add `Serializable` to the `extends` interface lists of `Collection`, `Cell`, `Keyed`, `Comparator`, `Predicate`, and `Function`.
2. Add the following code (courtesy of Simon Barber and Stefan Proels) to `HashedSet`

         private void readObject(java.io.ObjectInputStream stream)
              throws java.io.IOException, ClassNotFoundException
         {
           int len = stream.readInt();

           if (len > 0)
             table_ = new LLCell[len];
           else
             table_ = null;

           loadFactor_ = stream.readFloat();
           int count = stream.readInt();

           while (count-- > 0) {
             Object element = stream.readObject();
             int h = hashOf(element);
             LLCell hd = table_[h];
             LLCell n = new LLCell(element, hd);
             table_[h] = n;
           }
         }

         private void writeObject(java.io.ObjectOutputStream stream)
              throws java.io.IOException
         {
           int len;

           if (table_ != null)
             len = table_.length;
           else
             len = 0;

           stream.writeInt(len);
           stream.writeFloat(loadFactor_);
           stream.writeInt(count_);

           if (len > 0) {
             Enumeration e = elements();
             while (e.hasMoreElements()) stream.writeObject(e.nextElement());
           }
         }

3. Similarly for `HashedMap`, adding:

         private void readObject(java.io.ObjectInputStream stream)
              throws java.io.IOException, ClassNotFoundException
         {
           int len = stream.readInt();

           if (len > 0)
             table_ = new LLPair[len];
           else
             table_ = null;

           loadFactor_ = stream.readFloat();
           int count = stream.readInt();

           while (count-- > 0) {
             Object key = stream.readObject();
             Object element = stream.readObject();
             int h = hashOf(key);
             LLPair hd = table_[h];
             LLPair n = new LLPair(key, element, hd);
             table_[h] = n;
           }
         }

         private void writeObject(java.io.ObjectOutputStream stream)
              throws java.io.IOException
         {
           int len;

           if (table_ != null)
             len = table_.length;
           else
             len = 0;

           stream.writeInt(len);
           stream.writeFloat(loadFactor_);
           stream.writeInt(count_);

           if (len > 0) {
             Enumeration keys = keys();
             Enumeration elements = elements();
             while (keys.hasMoreElements()) {
       	stream.writeObject(keys.nextElement());
       	stream.writeObject(elements.nextElement());
             }
           }
         }

Please let me know if you encounter any problems. While these appear to to be the minimal changes, other read and write methods will probably be written for other collection classes in the next version.

Last modified: Wed Mar 12 05:25:37 EST