java Map 전환 Object와 Object 전환 맵 구현 코드

4308 단어 javaMapObject
java Map에서 Object와 Object로 맵 바꾸기
1. 솔리드 클래스를 정의합니다.


package reflect;
 
public class User {
  
 private String name;
  
 private int age;
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public int getAge() {
  return age;
 }
 
 public void setAge(int age) {
  this.age = age;
 }
}

2. 맵 전환 Object 변환은 다음과 같습니다.

package reflect;
 
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
 
public class testMapToObject {
  
 public static void main(String[] args){
   
  // Map 
  Map<string,object> map = new HashMap<string,object>();
  map.put("name", "Kobi");
  map.put("age", 39);
   
  User user = transferMapToUser(map,User.class);
   
  System.out.println(user.getName());
  System.out.println(user.getAge());
 }
  
 public static <t> T transferMapToUser(Map<string,object> map,Class<t> classT){
  try {
   return transfer(map,classT.newInstance());
  } catch (InstantiationException ex) {
   System.out.println("what the fuck?");
  } catch (IllegalAccessException ex) {
   System.out.println("what the fuck?");
  }
  return null;
 }
  
 public static <t> T transfer(Map<string,object> map,Object obj){
   
  try {
   BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
   PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
 
   for (PropertyDescriptor property : propertyDescriptors) {
    String key = property.getName();
    if (map.containsKey(key)) {
     Object value = map.get(key);
     //  property setter 
     Method setter = property.getWriteMethod();
     try {
      setter.invoke(obj, value);
     } catch (IllegalArgumentException ex) {
      System.out.println("what the fuck?");
     }
    }
   }
  } catch (Exception ex) {
   System.out.println("what the fuck?");
  }
 
  return (T) obj;
 }
  
}
실행 결과:

run:
Kobi
39
  ( : 0  )
3. Object 맵 전환:


package reflect;
 
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.TreeMap;
 
public class testObjectToMap {
  
 public static void main(String[] args){
  User user = new User();
  user.setName("Kobi");
  user.setAge(39);
   
  Map<string, object=""> map = transBean2Map(user);
  System.out.println(map);
   
 }
 
 public static Map<string, object=""> transBean2Map(Object obj) {
 
  if (obj == null) {
   return null;
  }
  TreeMap<string, object=""> map = new TreeMap<>();
  try {
   BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
   PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
   for (PropertyDescriptor property : propertyDescriptors) {
    String key = property.getName();
    //  class 
    if (key.equals("class")) {
     continue;
    }
     
    //  property getter 
    Method getter = property.getReadMethod();
    Object value = getter.invoke(obj);
     
    map.put(key, value);
   }
  } catch (Exception e) {
   System.out.println("transBean2Map Error " + e);
  }
  return map;
 }
}

실행 결과:


run:
{age=39, name=Kobi}
  ( : 0  )
읽어주셔서 감사합니다. 여러분에게 도움이 되었으면 좋겠습니다. 본 사이트에 대한 지지에 감사드립니다!

좋은 웹페이지 즐겨찾기