[转]Array和Collection相互转换以及copy深度的小测

http://www.micmiu.com/lang/java/array-collection-copy/

周末闲来无事,把java中Array和Collection之间相互转换小结了下,顺便对转换过程中涉及到的javabena的copy深度进行了简单的测试,详细内容将按照如下分类分别介绍:

  • List <-> Array
  • Set <-> Array
  • List <-> Set
  • List <-> Collection
  • Array Copy
  • List Copy

完整的代码:CollectionConvertTest.zip
测试过程中用到的javabean:TestVo的代码就不贴了,就一个简单的String属性name.

【一】、 List <-> Array

1 System.out.println("==============================");
2
3        // List -> Array
4        System.out.println("List -> Array ");
5        List<String> srclist = new ArrayList<String>();
6        srclist.add("src_list_a");
7        srclist.add("src_list_b");
8
9        String[] destArr = srclist.toArray(new String[srclist.size()]);
10        System.out.println("List -> Array 集合中元素的顺序不变");
11        srclist.clear();
12        System.out.println("srclist clear()");
13        System.out.println("destArr length=" + destArr.length);
14        if (destArr.length == 2) {
15            System.out.println("List -> Array for String : 深度copy");
16        } else {
17            System.out.println("List -> Array for String : 浅度copy");
18        }
19
20        List<TestVo> srcVolist = new ArrayList<TestVo>();
21        TestVo vo1 = new TestVo("michael");
22        srcVolist.add(vo1);
23        srcVolist.add(new TestVo("xiaoxiao"));
24        srcVolist.add(new TestVo("vo_3"));
25
26        TestVo[] destVoArr = srcVolist.toArray(new TestVo[srcVolist.size()]);
27        System.out.println("List -> Array 集合中元素的顺序不变");
28        vo1.setName("xiaoxiao_new");
29        System.out.println("srcVolist[0]的属性Name修改为:xiaoxiao_new");
30        System.out.println("destVoArr[0]的属性Name当前值:" + destVoArr[0].getName());
31        if ("xiaoxiao_new".equals(destVoArr[0].getName())) {
32            System.out.println("List -> Array for Bean : 浅度copy");
33        } else {
34            System.out.println("List -> Array for Bean : 深度copy");
35        }
36
37        // Array -> List
38        System.out.println("Array -> List");
39        String[] srcArr = new String[] { "src_arr_1", "src_arr_2" };
40        List<String> destList = Arrays.asList(srcArr);
41        srcArr[0] = "src_arr_1_new";
42        if ("src_arr_1_new".equals(destList.get(0))) {
43            System.out.println("Array -> List for String : 深度copy");
44        } else {
45            System.out.println("Array -> List for String : 浅度copy");
46        }
47
48        TestVo vo3 = new TestVo("vo_3");
49        TestVo[] srcObjArr = new TestVo[] { vo3, new TestVo("vo_4") };
50
51        List<TestVo> destObjList = Arrays.asList(srcObjArr);
52        vo3.setName("vo_3_new");
53        System.out.println(" srcObjArr[0]的属性Name修改为:vo_3_new");
54        System.out.println("destObjList[0]的属性Name当前值:"
55                + destObjList.get(0).getName());
56        if ("vo_3_new".equals(destObjList.get(0).getName())) {
57            System.out.println("Array -> List for Bean : 浅度copy");
58        } else {
59            System.out.println("Array -> List for Bean : 深度copy");
60        }
61        System.out.println("==============================");

运行结果:

1 ==============================
2 List -> Array
3 List -> Array 集合中元素的顺序不变
4 srclist clear()
5 destArr length=2
6 List -> Array for String : 深度copy
7 List -> Array 集合中元素的顺序不变
8 srcVolist[0]的属性Name修改为:xiaoxiao_new
9 destVoArr[0]的属性Name当前值:xiaoxiao_new
10 List -> Array for Bean : 浅度copy
11 Array -> List
12 Array -> List for String : 深度copy
13 srcObjArr[0]的属性Name修改为:vo_3_new
14 destObjList[0]的属性Name当前值:vo_3_new
15 Array -> List for Bean : 浅度copy
16 ==============================

【二】、 Set <-> Array

 

1 System.out.println("==============================");
2        // Set -> Array
3        System.out.println("Set -> Array");
4        System.out.println("Set -> Array 集合中元素的顺序会变");
5        Set<String> srcSet = new HashSet<String>();
6        srcSet.add("michael");
7        srcSet.add("xiaoxiao");
8        String[] destArr = srcSet.toArray(new String[srcSet.size()]);
9        System.out.println(" String destArr size: " + destArr.length);
10
11        Set<TestVo> srcObjSet = new HashSet<TestVo>();
12        TestVo vo1 = new TestVo("michael");
13        srcObjSet.add(vo1);
14        srcObjSet.add(new TestVo("xiaoxiao"));
15
16        TestVo[] destObjArr = srcObjSet.toArray(new TestVo[srcObjSet.size()]);
17        vo1.setName("michael_new");
18        System.out.println(" Bean destArr size: " + destArr.length);
19        boolean deepFlag = true;
20        for (TestVo vo : destObjArr) {
21            if ("michael_new".equals(vo.getName())) {
22                deepFlag = false;
23                break;
24            }
25        }
26        if (deepFlag) {
27            System.out.println("Set -> Array for Bean : 深度copy");
28        } else {
29            System.out.println("Set -> Array for Bean : 浅度copy");
30        }
31
32        // Array -> Set
33        System.out.println("Array -> Set");
34        System.out.println("Array -> Set 集合中元素的顺序会变");
35        String[] srcArr = new String[] { "michael", "xiaoxiao" };
36        Set<String> destSet = new HashSet<String>(Arrays.asList(srcArr));
37
38        System.out.println(" String destSet size: " + destSet.size());
39
40        TestVo vo3 = new TestVo("michael");
41        TestVo[] srcObjArr = new TestVo[] { vo1, new TestVo("xiaoxiao") };
42
43        Set<TestVo> destObjSet = new HashSet<TestVo>(Arrays.asList(srcObjArr));
44        vo3.setName("michael_new");
45        System.out.println(" Bean dest Set size: " + destObjSet.size());
46        deepFlag = true;
47        for (TestVo vo : destObjSet) {
48            if ("michael_new".equals(vo.getName())) {
49                deepFlag = false;
50                break;
51            }
52        }
53        if (deepFlag) {
54            System.out.println("Array -> Set for Bean : 深度copy");
55        } else {
56            System.out.println("Array -> Set for Bean : 浅度copy");
57        }
58
59        System.out.println("==============================");

执行结果:

1 ==============================
2 Set -> Array
3 Set -> Array 集合中元素的顺序会变
4 String destArr size: 2
5 Bean destArr size: 2
6 Set -> Array for Bean : 浅度copy
7 Array -> Set
8 Array -> Set 集合中元素的顺序会变
9 String destSet size: 2
10 Bean dest Set size: 2
11 Array -> Set for Bean : 浅度copy
12 ==============================

【三】、 List <-> Set

1 System.out.println("==============================");
2         // List -> Set
3         System.out.println("List -> Set");
4         System.out.println("List -> Set 集合中元素的顺序不变");
5         List<String> srclist = new ArrayList<String>();
6         srclist.add("src_list_a");
7         srclist.add("src_list_b");
8
9         Set<String> destSet = new HashSet<String>(srclist);
10         srclist.clear();
11         if (destSet.size() == 2) {
12             System.out.println("List -> Set for String : 深度copy");
13         } else {
14             System.out.println("List -> Set for String : 浅度copy");
15         }
16
17         List<TestVo> srcObjList = new ArrayList<TestVo>();
18         TestVo vo1 = new TestVo("michael");
19         srcObjList.add(vo1);
20         srcObjList.add(new TestVo("xiaoxiao"));
21
22         Set<TestVo> destObjectSet = new HashSet<TestVo>(srcObjList);
23         vo1.setName("michael_new");
24
25         if ("michael_new".equals(destObjectSet.iterator().next().getName())) {
26             System.out.println("List -> Set for Bean : 浅度copy");
27         } else {
28             System.out.println("List -> Set for Bean : 深度copy");
29         }
30
31         // Set -> List
32         System.out.println("Set -> List");
33         System.out.println("List -> Set 集合中元素的顺序会变");
34         Set<String> srcSet = new HashSet<String>();
35         srcSet.add("src_set_a");
36         srcSet.add("src_set_b");
37         List<String> destList = new ArrayList<String>(srcSet);
38         srcSet.clear();
39         if (destList.size() == 2) {
40             System.out.println("Set -> List for String : 深度copy");
41         } else {
42             System.out.println("Set -> List for String : 浅度copy");
43         }
44
45         Set<TestVo> srcObjSet = new HashSet<TestVo>();
46         TestVo vo3 = new TestVo("vo_3");
47         srcObjSet.add(vo3);
48         srcObjSet.add(new TestVo("vo_4"));
49
50         List<TestVo> destObjList = new ArrayList<TestVo>(srcObjSet);
51         vo3.setName("vo_3_new");
52         if ("vo_3_new".equals(destObjList.get(0).getName())
53                 || "vo_3_new".equals(destObjList.get(1).getName())) {
54             System.out.println("Set -> List for Bean : 浅度copy");
55         } else {
56             System.out.println("Set -> List for Bean : 深度copy");
57         }
58         System.out.println("==============================");

执行结果:

1 ==============================
2 List -> Set
3 List -> Set 集合中元素的顺序不变
4 List -> Set for String : 深度copy
5 List -> Set for Bean : 浅度copy
6 Set -> List
7 List -> Set 集合中元素的顺序会变
8 Set -> List for String : 深度copy
9 Set -> List for Bean : 浅度copy
10 ==============================

【四】、 List <-> Collection

1 System.out.println("==============================");
2       System.out.println("List -> Collection");
3       List<String> list = new ArrayList<String>();
4       list.add("michael");
5       list.add("xiaoxiao");
6       Collection<String> destCol = list;
7       System.out.println("destCol size:" + destCol.size());
8       System.out.println("List继承于Collection,可以直接赋值");
9
10       System.out.println("Collection -> List");
11       Collection<String> col = new ArrayList<String>();
12       col.add("michael");
13       col.add("xiaoxiao");
14       List<String> destList = new ArrayList<String>(col);
15       System.out.println("destList size:" + destList.size());
16
17       System.out.println("==============================");

执行结果:

1 ==============================
2 List -> Collection
3 destCol size:2
4 List继承于Collection,可以直接赋值
5 Collection -> List
6 destList size:2
7 ==============================

【五】、 Array Copy

1 System.out.println("==============================");
2         System.out.println("Array Copy :String");
3         String[] srcArr = new String[] { "michael", "xiaoxiao" };
4         String[] destArr = new String[srcArr.length];
5
6         System.arraycopy(srcArr, 0, destArr, 0, srcArr.length);
7
8         System.out.println("Array Copy : Bean");
9
10         TestVo vo1 = new TestVo("michael");
11         TestVo[] srcObjArr = new TestVo[] { vo1, new TestVo("xiaoxiao") };
12         TestVo[] destObjArr = new TestVo[srcArr.length];
13
14         System.arraycopy(srcObjArr, 0, destObjArr, 0, srcObjArr.length);
15         vo1.setName("michael_new");
16         System.out.println("srcObjArr[0]的属性name修改为:michael_new");
17         System.out
18                 .println("destObjArr[0]的属性name当前值:" + destObjArr[0].getName());
19         if (srcObjArr[0] == destObjArr[0]) {
20             System.out.println("Array Copy for Bean : 浅度copy");
21         } else {
22             System.out.println("Array Copy for Bean : 深度copy");
23         }
24         System.out.println("==============================");

执行结果:

1 ==============================
2 Array Copy :String
3 Array Copy : Bean
4 srcObjArr[0]的属性name修改为:michael_new
5 destObjArr[0]的属性name当前值:michael_new
6 Array Copy for Bean : 浅度copy
7 ==============================

【六】、 List Copy

1 System.out.println("==============================");
2         System.out.println("List Copy :String");
3
4         List<String> srcList = new ArrayList<String>();
5         srcList.add("michael");
6         srcList.add("xiaoxiao");
7
8         List<String> descList = srcList;
9
10         List<String> descList2 = new ArrayList<String>(srcList);
11
12         List<String> descList3 = new ArrayList<String>(Arrays
13                 .asList(new String[srcList.size()]));
14         Collections.copy(descList3, srcList);
15
16         srcList.clear();
17         System.out.println("源List清空");
18         System.out.println("直接赋值descList = srcList 的方法copy后的List     size: "
19                 + descList.size());
20         System.out.println("new ArrayList<String>(srcList)的方法copy后的List size: "
21                 + descList2.size());
22         System.out.println("Collections.copy(dest,src)的方法copy后的List     size: "
23                 + descList3.size());
24
25         System.out.println("==============================");

执行结果:

1 ==============================
2 List Copy :String
3 源List清空
4 直接赋值descList = srcList 的方法copy后的List     size: 0
5 new ArrayList<String>(srcList)的方法copy后的List size: 2
6 Collections.copy(dest,src)的方法copy后的List     size: 2
7 ==============================
此条目发表在 Java 分类目录,贴了 , , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>