BeanUtils和BeanMap对象相互转换 对比

BeanUtils和BeanMap对象相互转换 对比

 

 可以使用BeanMap,进行Bean对象和Map相互转换,也可以用BeanUtil的方式进行Bean对象和Map相互转换,两种之间的效率如何呢? 该如何选择?

 

 

1,对比Bean对象转换为List的情况

测试:

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 JOSN change beans to list ===========");
    testBeansToList();
    changeBeansToListMap();
}

public static void testBeansToList(){
    System.out.println("========== BeanMapUtils change beans to listMap ============");
    long startTime = System.currentTimeMillis();
    BeanMapUtils.changeBeansToList(shapeBeanList);
    long endTime = System.currentTimeMillis();
    System.out.println("BeanMapUtils.listsToBeans转换耗费的时间:" + (endTime - startTime) + "ms");
}

public static void changeBeansToListMap() {
    List mapList=new ArrayList<>();
    System.out.println("========== JSON change beans to list ============");
    long startTime = System.currentTimeMillis();
    for(Object shape: shapeBeanList){
        Map map = JSON.parseObject(JSONObject.toJSONString(shape), Map.class);
        mapList.add(map);
    }
    long endTime = System.currentTimeMillis();
    System.out.println("JSON BeanToListMap 转换耗费的时间:" + (endTime - startTime) + "ms");
}

 

将size改为: 500000

 

结果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

 

将size改为: 1000000

 

结果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

 

将size改为: 2000000

 

结果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处理性能会更好。

 

2,对比 将List转换为Bean对象

 

测试:

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");
}

 

size: 500000

结果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

size: 1000000

结果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

size: 2000000

结果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更简洁,而且还有更多好用的功能

 

你可能感兴趣的:(java,BeanUtil,BeanMap,性能对比)