一、前言
基于granite源码包org.granite.collections.BasicMap继承java.util.HashMap,并实现对象序列化接口java.io.Externalizable,具体参见源码说明。
二、源码说明
package org.granite.collections;@b@@b@import java.io.Externalizable;@b@import java.io.IOException;@b@import java.io.ObjectInput;@b@import java.io.ObjectOutput;@b@import java.io.Serializable;@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@@b@public class BasicMap<K, V> extends HashMap<K, V>@b@ implements Externalizable, Serializable@b@{@b@ public BasicMap()@b@ {@b@ }@b@@b@ public BasicMap(Map<K, V> map)@b@ {@b@ super(map);@b@ }@b@@b@ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException@b@ {@b@ Object[] pairs = (Object[])in.readObject();@b@ if (pairs != null)@b@ for (Object pair : pairs)@b@ put(((Object[])pair)[0], ((Object[])pair)[1]);@b@ }@b@@b@ public void writeExternal(ObjectOutput out) throws IOException@b@ {@b@ Object[] outObjectArray = new Object[size()];@b@@b@ int index = 0;@b@ for (Map.Entry entry : entrySet())@b@ outObjectArray[(index++)] = { entry.getKey(), entry.getValue() };@b@@b@ out.writeObject(outObjectArray);@b@ }@b@@b@ public static BasicMap<?, ?> newInstance(Map<?, ?> map)@b@ {@b@ return new BasicMap(map);@b@ }@b@@b@ public String toString()@b@ {@b@ return getClass().getName() + " " + super.toString();@b@ }@b@}