Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the conversion between object and Map by BeanMap in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to achieve the conversion between objects and Map in Java". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "how to convert objects and Map in Java" can help you solve the problem.

There are many ways to convert javabean to map, such as:

1. First convert bean to json through ObjectMapper, and then convert json to map, but this method is relatively winding, and the efficiency is very low. After testing, it takes 12 seconds to convert 10000 bean in a loop! Not recommended

2. Get the properties and values of the bean class through Java reflection, and then convert them to the key-value pair corresponding to map. This method takes the second place, but it is a little troublesome.

3. Through the method in the net.sf.cglib.beans.BeanMap class, this method is extremely efficient. The difference between this method and the second method is that the cache is used, which requires initialization when the bean is created for the first time, and then the cache is used later, so the speed is extremely fast. After testing, it takes only about 300ms to convert bean and map for 10000 times.

Therefore, the third way is recommended. The following is the relevant code:

/ * replace the object with map* @ param bean* @ return*/public static Map beanToMap (T bean) {Map map = Maps.newHashMap (); if (bean! = null) {BeanMap beanMap = BeanMap.create (bean); for (Object key: beanMap.keySet ()) {map.put (key+ ", beanMap.get (key));} return map } / * convert map to javabean object * @ param map* @ param bean* @ return*/public static T mapToBean (Map map,T bean) {BeanMap beanMap = BeanMap.create (bean); beanMap.putAll (map); return bean;} / * * convert List to List* @ param objList* @ return* @ throws JsonMappingException* @ throws IOException*/public static List objectsToMaps (List objList) {List list = Lists.newArrayList () If (objList! = null & & objList.size () > 0) {Map map = null;T bean = null;for (int I = 0pr size = objList.size (); I

< size; i++) {bean = objList.get(i);map = beanToMap(bean);list.add(map);}}return list;}/*** 将List转换为List* @param maps* @param clazz* @return* @throws InstantiationException* @throws IllegalAccessException*/public static List mapsToObjects(List maps,Class clazz) throws InstantiationException, IllegalAccessException {List list = Lists.newArrayList();if (maps != null && maps.size() >

0) {Map map = null;T bean = null;for (int I = 0Magna size = maps.size (); I < size; itemized +) {map = maps.get (I); bean = clazz.newInstance (); mapToBean (map, bean); list.add (bean);}} return list;} on "how BeanMap converts objects and Map in Java", that's it. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report