Java programming Java Object copy

Source: Internet
Author: User
Tags serialization shallow copy

(1) Cloning

Implements Cloneable

(2) serialization

Implements Serializable

(3) Copy with reflection mechanism

Apache's Beanutils Solution

When using Org.apache.commons.beanutils.BeanUtils for object-deep copying, the main reason is to inject a new type converter into the beanutils framework, because by default, Beanutils is a reference to a complex object's replication,

It can be found that when you copy a reference using Org.apache.commons.beanutils.BeanUtils, the primary and source references are the same, that is, changing the reference property of the master affects the reference to the source, so this is a shallow copy

Apache's Propertyutils Solution

The Propertyutils copyproperties () method is almost identical to beanutils.copyproperties (), the main difference being that the latter provides the type conversion function, that is, when two javabean of the same name attribute are found to be of different types, Propertyutils does not support this feature in the range of supported data types, so it is more common to say that beanutils is less risky to err. And it still belongs to the shallow copy.

Apache provides Serializationutils.clone (t), T object needs to implement Serializable interface, he belongs to deep clone.

Spring's Beanutils Solution

The beanutils in spring, which is implemented simply by simply get/set the properties of the same name in two objects, checks only the accessibility of the property.

As you can see, the member variable assignment is based on the list of members of the target object and skips the ignore and does not exist in the source object, so this method is safe and does not cause an error because of the structural differences between the two objects, but you must guarantee that the two member variable types with the same name are the same.

Dozer

Dozer (http://dozer.sourceforge.net/) enables deep copy. Dozer is a reflection-based implementation of object copies, reflection calls Set/get, or directly assigns a value to a member variable. This method is assigned by invoke execution, which is usually implemented using Beanutil, javassist and other open source libraries.

Mapstrcut

Mapstrcut is a compile-time object replication scheme that dynamically generates the class file of the Set/get code and calls the class file directly at run time. This way actually throws the Set/get code that exists, just doesn't need to write it yourself.

Beancopier

You can improve performance by caching instances of Beancopier.

Fastjson and Gson

Using Fastjson and Gson mainly through object JSON serialization and deserialization to complete object replication, here is just a way to provide a different copy of the object, the example is slightly.

Spring's beanutils is relatively stable, not because of the amount of large, time-consuming significantly increased, but in fact, the benchmark time is longer, Apache beanutils stability and efficiency are not, not to take, Gson, because do two gson conversion, so normal project, It may take less time, pojoutils stable than spring, but the overall time-consuming advantage is obvious because it is simply a conversion template based on the needs of the project, and this code is available in several other tool classes.

In other blogs on the web (see Reference), the beancopier of Apache's beanutils, Propertyutils, and Cglib were tested for performance.

Test results:

Performance comparison: Beancopier > Beanutils. The performance of Beancopier is higher than the other two 100 orders of magnitude.

Recommended Use:

    1. Beanutils (simple, easy to use)
    2. Beancopier (the performance is close to the manual set after the cache is added)
    3. Dozer (deep copy)
    4. Fastjson (used in specific scenarios)

Java programming Java Object copy

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.