Three ways to convert Object to Map
is to map the object and its corresponding property name and attribute values to a map
Import Java.beans.BeanInfo;
Import java.beans.IntrospectionException;
Import Java.beans.Introspector;
Import Java.beans.PropertyDescriptor;
Import Java.lang.reflect.Field;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import Java.util.HashMap;
Import Java.util.Map; /** * User:pengfei.hpf * date:14-5-8 * Time: PM 12:34 */public class Objecttomap { &NBSP ; public static void Main (string[] args) throws Introspectionexception, InvocationTargetException, illegalaccessexception { testobject o = new Testobject (); O.SETCIT
Y ("Beijing");
o.setname ("BJ");
O.setorder ("ASC");
Objecttomap objecttomap = new Objecttomap ();
SYSTEM.OUT.PRINTLN ("=========== Invoke objecttomapviabeaninfo ==========="); Objecttomap.objecttomapviabeaninfo (o);
SYSTEM.OUT.PRINTLN ("=========== Invoke objecttomapviafields ===========");
Objecttomap.objecttomapviafields (o);
SYSTEM.OUT.PRINTLN ("=========== Invoke objecttomapviaapachetools ===========");
Objecttomapviaapachetools (o); } /** * introspector replication attribute value */ p rivate static void Objecttomapviabeaninfo (Testobject o) throws Introspectionexception, InvocationTargetException, illegalaccessexception { map<string, object> result = new hashmap<string, object> (
);
BeanInfo info = introspector.getbeaninfo (O.getclass ()); for (PropertyDescriptor pd:info.getPropertyDescriptors ()) { &NBS P
Method reader = Pd.getreadmethod (); //content NULLFilter out if (reader = null | | Reader.invoke (o) = null) { &NBS P
continue; //default Inherit object class properties, filter out &N Bsp if (Pd.getname (). Equalsignorecase ("class") {
Continue Result.put (Pd.getname (), Reader.invo
Ke (o)); } for (String Key:result.keySet ()) { &NBSP ; SYSTEM.OUT.PRINTLN ("key:" + key + ";
Value: "+ result.get (key)); } { /** * copy property values Getdeclaredfields way &nbs P * getdeclaredfields method does not return properties of the parent class */ private static void ObjecttomapViafields (Testobject o) throws illegalaccessexception { map<string, object> resmap = new
Hashmap<string, object> ();
field[] Declaredfields = O.getclass (). Getdeclaredfields (); for (Field Field:declaredfields) { field.setaccess
Ible (TRUE);
//filter content empty if (Field.get (o) = null) {
continue; Resmap.put (Field.getname (), field.ge
T (o));
} PrintOut (RESMAP); } /** * via Apache beanutils replication attribute value * its internal implementation Method 1 &N Bsp * * private static void Objecttomapviaapachetools (Testobject o) { &NBsp
map<string, object> resmap = new Org.apache.commons.beanutils.BeanMap (o);
PrintOut (RESMAP); } /** * printing results */ private static void PrintOut (map<string, object> resmap) { for (String Key:resMap.keySet ()) { &NBSP ; SYSTEM.OUT.PRINTLN ("key:" + key + ";
Value: "+ resmap.get (key)); } { /** * test class * *   ;
public static class Testobject { strings city; string name;
String order; public String GetOrder () { -order; &NBS P } public void Setorder (String order) { This.order = order; } public String getName ( { return name; } Publ IC void SetName (String name) { this.name = name;
Public
String getcity () {return City
;
Public
void Setcity (String city) {
this.city = city;
}
}
}