Cloneable and serializable reasons are not implemented in Java collection classes

Source: Internet
Author: User
Tags serialization

Cloneable and serializable reasons are not implemented in Java collection classes

  

Inexplicable to be asked such a question, also can not answer, record, why the Java Collection class does not implement cloneable and serializable interface.

  

1. Thecloneable interface function is to copy the property value of one object to another object, rather than a reference to the object.

  

2.Serializable interface function (this wordy)

2.1 Use of serialization

1. Sometimes, if you want an object to persist (save to disk), or to make a remote object call, use serialization to achieve these effects.

We have to implement the serializable interface for all classes that support persistent storage, and also to deserialize when reading.

2. For the JVM, the persisted class must have a tag that implements the serializable interface, which is associated with Serialversionuid, which is in the deserialization session

Make sure to load the object with that class.

3. It is worthwhile to note that persisted data is present in the Java heap, and the static type of data exists in the method area and cannot be persisted. If you do not want to have a member variable

Persistent, variable preceded by transient keyword

4. Of course the serialized Serialversionuid This can also be customized

3. Back to the topic of this post, why does the collection class do not implement the above two interfaces?

In fact, it is not difficult to see that cloneable is a copy of the object, serialization is also for the operation of the object, the collection class is just a tool to manage the object, like the list can be a linear management object,

Set sets are able to weigh objects, and these collection classes are created for and for managing objects.

In fact, both interfaces are objects that are really objects, not management objects such as collection classes. This is semantically the Cloneable interface and serializable interface of the collection class.

The specific type implementation in the collection, rather than the collection class, should be implemented for serialization.

Assuming that the collection class implements both interfaces, if I want to generate a collection that does not need to be serialized and does not need a clone, then the collection class is enforced, which violates the design principle of the collection.

Ref: 50556966

Ref: 76461517

    

Cloneable and serializable reasons are not implemented in Java collection classes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.