Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class Main { /** * Get all fields' value and put them to a map. * * @param bean * @return Map */ public static Map<String, Object> getFieldValueMap(Object bean) { Class<?> cls = bean.getClass(); Map<String, Object> valueMap = new HashMap<String, Object>(); // Get all fields. Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); Object value = field.get(bean); // if(value == null) { // valueMap.put(field.getName(), ""); // continue; // } valueMap.put(field.getName(), value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return valueMap; } }