九州编程
我测试了Jackson和BeanUtils,发现BeanUtils要快得多。在我的机器(Windows8.1,JDK1.7)中,我得到了这个结果。BeanUtils t2-t1 = 286Jackson t2-t1 = 2203public class MainMapToPOJO {public static final int LOOP_MAX_COUNT = 1000;public static void main(String[] args) { Map<String, Object> map = new HashMap<>(); map.put("success", true); map.put("data", "testString"); runBeanUtilsPopulate(map); runJacksonMapper(map);}private static void runBeanUtilsPopulate(Map<String, Object> map) { long t1 = System.currentTimeMillis(); for (int i = 0; i < LOOP_MAX_COUNT; i++) { try { TestClass bean = new TestClass(); BeanUtils.populate(bean, map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } long t2 = System.currentTimeMillis(); System.out.println("BeanUtils t2-t1 = " + String.valueOf(t2 - t1));}private static void runJacksonMapper(Map<String, Object> map) { long t1 = System.currentTimeMillis(); for (int i = 0; i < LOOP_MAX_COUNT; i++) { ObjectMapper mapper = new ObjectMapper(); TestClass testClass = mapper.convertValue(map, TestClass.class); } long t2 = System.currentTimeMillis(); System.out.println("Jackson t2-t1 = " + String.valueOf(t2 - t1));}}