On the mutual transformation between object and map

There are many ways to convert JavaBeans to maps, such as:

1. Through objectmapper, first convert beans to JSON, and then convert JSON to map, but this method is relatively convoluted and inefficient. After testing, it takes 12 seconds to convert 10000 beans in a loop!!! Not recommended

2. Get the attributes and values of the bean class through java reflection, and then convert them to the key value pairs corresponding to the map. This method is second, but it is a little troublesome

3. Through net sf. cglib. beans. The method in the beanmap class is very efficient. The difference between this method and the second method is that the cache is used. The bean needs to be initialized when it is first created, and then the cache is used, so the speed is very fast. After testing, the conversion of the circular bean and map 10000 times takes only about 300 milliseconds.

Therefore, the third method is recommended. The following are the relevant codes:

That's all for the introduction and code of using beanmap to convert objects and maps. I hope it can be helpful to you. If you have any questions, you can leave me a message and Xiaobian will reply to you in time. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>