A tool class for JavaBean and map conversions based on Java reflection

Source: Internet
Author: User
Tags object object

Words don't say much, the code is as follows

Package Com.study;import java.lang.reflect.field;import Java.util.hashmap;import java.util.map;/** * PackageName Com.study * Description please write Description. * USER:YH * time:2016/11/29 16:57 */public class maputil{/** * JavaBean Turn Map * @param object JavaBean * @return Conversion result MAP * @throws Exception */public static map<string, object> Beantomap (O        Bject object) throws Exception {map<string, object> Map = new hashmap<string, object> ();        Class cls = Object.getclass ();        field[] fields = Cls.getdeclaredfields ();            for (Field field:fields) {field.setaccessible (true);        Map.put (Field.getname (), Field.get (object));    } return map; }/** * * @param map required to convert map * @param CLS Target JavaBean class object * @return Target class ' object ' @throws Except Ion */public static Object Maptobean (map<string, object> Map, Class cls) throws Exception {       Object object = Cls.newinstance ();            For (String Key:map.keySet ()) {Field temfiels = Cls.getdeclaredfield (key);            Temfiels.setaccessible (TRUE);        Temfiels.set (object, Map.get (key));    } return object; }}

  

A tool class for JavaBean and map conversions based on Java reflection

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.