import java
.io
.ByteArrayInputStream
;
import java
.io
.ByteArrayOutputStream
;
import java
.io
,IOException
;
import java
.io
.ObjectInputStream
;
import java
.io
.ObjectOutputStream
;
import java
.io
.Serializable
;
public class Node<T> implements Serializable{
private static final long serialVersionUID
= 1636144465925665265L
;
public T data
;
public Node
<T> next
;
public Node(T data
, Node
<T> next
)
{
try {
ByteArrayOutputStream baos
= new ByteArrayOutputStream();
ObjectOutputStream oos
= new ObjectOutputStream(baos
);
oos
.writeObject(data
);
ByteArrayInputStream bais
= new ByteArrayInputStream(baos
.toByteArray());
ObjectInputStream ois
= new ObjectInputStream(bais
);
T data1
;
data1
= (T
) ois
.readObject();
this.data
= data1
;
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} catch (IOException e
) {
e
.printStackTrace();
}
this.next
= next
;
}
public Node()
{
this(null
, null
);
}
public String
toString()
{
return this.data
.toString();
}
@SuppressWarnings("unchecked")
public boolean equals(Object obj
)
{
return obj
==this || obj
instanceof Node<?> && this.data
.equals(((Node
<T>)obj
).data
);
}
}
通过实现Serializable接口来实现对象的不可变性,然后通过写入ObjectOutputStream写入流,在通过ByteArrayInputStream读入就可以,使得重新获得的对象和原来的对象不同。