可以使用BeanMap,进行Bean对象和Map相互转换,也可以用BeanUtil的方式进行Bean对象和Map相互转换,两种之间的效率如何呢? 该如何选择?
private final static int SIZE = 500000;
private static List
结果1:
======== compare BeanMap and BeanUtil change beans to list ===========
========== BeanMapUtils change beans to listMap ============
BeanMapUtils.beansToMaps 转换耗费的时间:938ms
========== BeanUtils change beans to listMap ============
BeanUtils.listsToBeans 转换耗费的时间:593ms
结果2:
======== compare BeanMap and BeanUtil change beans to list ===========
========== BeanMapUtils change beans to listMap ============
BeanMapUtils.beansToMaps 转换耗费的时间:989ms
========== BeanUtils change beans to listMap ============
BeanUtils.listsToBeans 转换耗费的时间:516ms
结果3:
======== compare BeanMap and BeanUtil change beans to list ===========
========== BeanMapUtils change beans to listMap ============
BeanMapUtils.beansToMaps 转换耗费的时间:908ms
========== BeanUtils change beans to listMap ============
BeanUtils.listsToBeans 转换耗费的时间:1040ms
结果4:
======== compare BeanMap and BeanUtil change beans to list ===========
========== BeanMapUtils change beans to listMap ============
BeanMapUtils.beansToMaps 转换耗费的时间:973ms
========== BeanUtils change beans to listMap ============
BeanUtils.listsToBeans 转换耗费的时间:1006ms
结果5:
======== compare BeanMap and BeanUtil change beans to list ===========
========== BeanMapUtils change beans to listMap ============
BeanMapUtils.beansToMaps 转换耗费的时间:2129ms
========== BeanUtils change beans to listMap ============
BeanUtils.listsToBeans 转换耗费的时间:2683ms
结果6:
======== compare BeanMap and BeanUtil change beans to list ===========
========== BeanMapUtils change beans to listMap ============
BeanMapUtils.beansToMaps 转换耗费的时间:2222ms
========== BeanUtils change beans to listMap ============
BeanUtils.listsToBeans 转换耗费的时间:3051ms
有结果可以看出,小于1百万的时候,BeanUtils处理把beans转为list的时候,效率会更好,之后的话,BeanMap处理性能会更好。
private final static int SIZE = 500000;
private static List> shapeLists = ShapeListMapFactory.create(SIZE);
private static List shapeBeanList = ShapeFactory.create(SIZE);
public static void main(String[] args) {
System.out.println("======== compare BeanMap and BeanUtil change list to beans ===========");
testBeanMapListToBeans();
testBeanUtilsListMapToBeans();
}
public static void testBeanMapListToBeans(){
System.out.println("======== BeanMapUtils change listMap to beans ========");
long startTime = System.currentTimeMillis();
BeanMapUtils.changeListToBeans(shapeLists, Shape.class);
long endTime = System.currentTimeMillis();
System.out.println("BeanMapUtils.mapsToBeans 转换耗费的时间:" + (endTime - startTime) + "ms");
}
public static void testBeanUtilsListMapToBeans() {
System.out.println("========== BeanUtils change list to Bean ============");
long startTime = System.currentTimeMillis();
BeanUtils.listsToBeans(shapeLists, Shape.class);
long endTime = System.currentTimeMillis();
System.out.println("BeanUtils listsToBeans 转换耗费的时间:" + (endTime - startTime) + "ms");
}
结果1:
======== compare BeanMap and BeanUtil change list to beans ===========
======== BeanMapUtils change listMap to beans ========
BeanMapUtils.mapsToBeans 转换耗费的时间:1114ms
========== BeanUtils change list to Bean ============
BeanUtils listsToBeans 转换耗费的时间:530ms
结果2:
======== compare BeanMap and BeanUtil change list to beans ===========
======== BeanMapUtils change listMap to beans ========
BeanMapUtils.mapsToBeans 转换耗费的时间:1078ms
========== BeanUtils change list to Bean ============
BeanUtils listsToBeans 转换耗费的时间:511ms
结果3:
======== compare BeanMap and BeanUtil change list to beans ===========
======== BeanMapUtils change listMap to beans ========
BeanMapUtils.mapsToBeans 转换耗费的时间:954ms
========== BeanUtils change list to Bean ============
BeanUtils listsToBeans 转换耗费的时间:868ms
结果4:
======= compare BeanMap and BeanUtil change list to beans ===========
======== BeanMapUtils change listMap to beans ========
BeanMapUtils.mapsToBeans 转换耗费的时间:958ms
========== BeanUtils change list to Bean ============
BeanUtils listsToBeans 转换耗费的时间:874ms
结果5:
======== compare BeanMap and BeanUtil change list to beans ===========
======== BeanMapUtils change listMap to beans ========
BeanMapUtils.mapsToBeans 转换耗费的时间:1708ms
========== BeanUtils change list to Bean ============
BeanUtils listsToBeans 转换耗费的时间:2245ms
结果6:
======== compare BeanMap and BeanUtil change list to beans ===========
======== BeanMapUtils change listMap to beans ========
BeanMapUtils.mapsToBeans 转换耗费的时间:1862ms
========== BeanUtils change list to Bean ============
BeanUtils listsToBeans 转换耗费的时间:2011ms
有结果可以看出,跟把Bean对象转为List的情况相似。小数量的BeanUtil更有优势。
比较完两者的效率,在数据量很大的情况下,进行Bean转为为List的情况下,BeanMap的方式比较合适。不过实际中一般也不会进行那样大量的转换。 平时使用BeanUtil就好了,引用的代码也比BeanMap更简洁,而且还有更多好用的功能