周末闲来无事,把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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
System.out.println("=============================="); // List -> Array System.out.println("List -> Array "); List<String> srclist = new ArrayList<String>(); srclist.add("src_list_a"); srclist.add("src_list_b"); String[] destArr = srclist.toArray(new String[srclist.size()]); System.out.println("List -> Array 集合中元素的顺序不变"); srclist.clear(); System.out.println("srclist clear()"); System.out.println("destArr length=" + destArr.length); if (destArr.length == 2) { System.out.println("List -> Array for String : 深度copy"); } else { System.out.println("List -> Array for String : 浅度copy"); } List<TestVo> srcVolist = new ArrayList<TestVo>(); TestVo vo1 = new TestVo("michael"); srcVolist.add(vo1); srcVolist.add(new TestVo("xiaoxiao")); srcVolist.add(new TestVo("vo_3")); TestVo[] destVoArr = srcVolist.toArray(new TestVo[srcVolist.size()]); System.out.println("List -> Array 集合中元素的顺序不变"); vo1.setName("xiaoxiao_new"); System.out.println("srcVolist[0]的属性Name修改为:xiaoxiao_new"); System.out.println("destVoArr[0]的属性Name当前值:" + destVoArr[0].getName()); if ("xiaoxiao_new".equals(destVoArr[0].getName())) { System.out.println("List -> Array for Bean : 浅度copy"); } else { System.out.println("List -> Array for Bean : 深度copy"); } // Array -> List System.out.println("Array -> List"); String[] srcArr = new String[] { "src_arr_1", "src_arr_2" }; List<String> destList = Arrays.asList(srcArr); srcArr[0] = "src_arr_1_new"; if ("src_arr_1_new".equals(destList.get(0))) { System.out.println("Array -> List for String : 深度copy"); } else { System.out.println("Array -> List for String : 浅度copy"); } TestVo vo3 = new TestVo("vo_3"); TestVo[] srcObjArr = new TestVo[] { vo3, new TestVo("vo_4") }; List<TestVo> destObjList = Arrays.asList(srcObjArr); vo3.setName("vo_3_new"); System.out.println(" srcObjArr[0]的属性Name修改为:vo_3_new"); System.out.println("destObjList[0]的属性Name当前值:" + destObjList.get(0).getName()); if ("vo_3_new".equals(destObjList.get(0).getName())) { System.out.println("Array -> List for Bean : 浅度copy"); } else { System.out.println("Array -> List for Bean : 深度copy"); } System.out.println("=============================="); |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
============================== List -> Array List -> Array 集合中元素的顺序不变 srclist clear() destArr length=2 List -> Array for String : 深度copy List -> Array 集合中元素的顺序不变 srcVolist[0]的属性Name修改为:xiaoxiao_new destVoArr[0]的属性Name当前值:xiaoxiao_new List -> Array for Bean : 浅度copy Array -> List Array -> List for String : 深度copy srcObjArr[0]的属性Name修改为:vo_3_new destObjList[0]的属性Name当前值:vo_3_new Array -> List for Bean : 浅度copy ============================== |
【二】、 Set <-> Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
System.out.println("=============================="); // Set -> Array System.out.println("Set -> Array"); System.out.println("Set -> Array 集合中元素的顺序会变"); Set<String> srcSet = new HashSet<String>(); srcSet.add("michael"); srcSet.add("xiaoxiao"); String[] destArr = srcSet.toArray(new String[srcSet.size()]); System.out.println(" String destArr size: " + destArr.length); Set<TestVo> srcObjSet = new HashSet<TestVo>(); TestVo vo1 = new TestVo("michael"); srcObjSet.add(vo1); srcObjSet.add(new TestVo("xiaoxiao")); TestVo[] destObjArr = srcObjSet.toArray(new TestVo[srcObjSet.size()]); vo1.setName("michael_new"); System.out.println(" Bean destArr size: " + destArr.length); boolean deepFlag = true; for (TestVo vo : destObjArr) { if ("michael_new".equals(vo.getName())) { deepFlag = false; break; } } if (deepFlag) { System.out.println("Set -> Array for Bean : 深度copy"); } else { System.out.println("Set -> Array for Bean : 浅度copy"); } // Array -> Set System.out.println("Array -> Set"); System.out.println("Array -> Set 集合中元素的顺序会变"); String[] srcArr = new String[] { "michael", "xiaoxiao" }; Set<String> destSet = new HashSet<String>(Arrays.asList(srcArr)); System.out.println(" String destSet size: " + destSet.size()); TestVo vo3 = new TestVo("michael"); TestVo[] srcObjArr = new TestVo[] { vo1, new TestVo("xiaoxiao") }; Set<TestVo> destObjSet = new HashSet<TestVo>(Arrays.asList(srcObjArr)); vo3.setName("michael_new"); System.out.println(" Bean dest Set size: " + destObjSet.size()); deepFlag = true; for (TestVo vo : destObjSet) { if ("michael_new".equals(vo.getName())) { deepFlag = false; break; } } if (deepFlag) { System.out.println("Array -> Set for Bean : 深度copy"); } else { System.out.println("Array -> Set for Bean : 浅度copy"); } System.out.println("=============================="); |
执行结果:
1 2 3 4 5 6 7 8 9 10 11 12 |
============================== Set -> Array Set -> Array 集合中元素的顺序会变 String destArr size: 2 Bean destArr size: 2 Set -> Array for Bean : 浅度copy Array -> Set Array -> Set 集合中元素的顺序会变 String destSet size: 2 Bean dest Set size: 2 Array -> Set for Bean : 浅度copy ============================== |
【三】、 List <-> Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
System.out.println("=============================="); // List -> Set System.out.println("List -> Set"); System.out.println("List -> Set 集合中元素的顺序不变"); List<String> srclist = new ArrayList<String>(); srclist.add("src_list_a"); srclist.add("src_list_b"); Set<String> destSet = new HashSet<String>(srclist); srclist.clear(); if (destSet.size() == 2) { System.out.println("List -> Set for String : 深度copy"); } else { System.out.println("List -> Set for String : 浅度copy"); } List<TestVo> srcObjList = new ArrayList<TestVo>(); TestVo vo1 = new TestVo("michael"); srcObjList.add(vo1); srcObjList.add(new TestVo("xiaoxiao")); Set<TestVo> destObjectSet = new HashSet<TestVo>(srcObjList); vo1.setName("michael_new"); if ("michael_new".equals(destObjectSet.iterator().next().getName())) { System.out.println("List -> Set for Bean : 浅度copy"); } else { System.out.println("List -> Set for Bean : 深度copy"); } // Set -> List System.out.println("Set -> List"); System.out.println("List -> Set 集合中元素的顺序会变"); Set<String> srcSet = new HashSet<String>(); srcSet.add("src_set_a"); srcSet.add("src_set_b"); List<String> destList = new ArrayList<String>(srcSet); srcSet.clear(); if (destList.size() == 2) { System.out.println("Set -> List for String : 深度copy"); } else { System.out.println("Set -> List for String : 浅度copy"); } Set<TestVo> srcObjSet = new HashSet<TestVo>(); TestVo vo3 = new TestVo("vo_3"); srcObjSet.add(vo3); srcObjSet.add(new TestVo("vo_4")); List<TestVo> destObjList = new ArrayList<TestVo>(srcObjSet); vo3.setName("vo_3_new"); if ("vo_3_new".equals(destObjList.get(0).getName()) || "vo_3_new".equals(destObjList.get(1).getName())) { System.out.println("Set -> List for Bean : 浅度copy"); } else { System.out.println("Set -> List for Bean : 深度copy"); } System.out.println("=============================="); |
执行结果:
1 2 3 4 5 6 7 8 9 10 |
============================== List -> Set List -> Set 集合中元素的顺序不变 List -> Set for String : 深度copy List -> Set for Bean : 浅度copy Set -> List List -> Set 集合中元素的顺序会变 Set -> List for String : 深度copy Set -> List for Bean : 浅度copy ============================== |
【四】、 List <-> Collection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
System.out.println("=============================="); System.out.println("List -> Collection"); List<String> list = new ArrayList<String>(); list.add("michael"); list.add("xiaoxiao"); Collection<String> destCol = list; System.out.println("destCol size:" + destCol.size()); System.out.println("List继承于Collection,可以直接赋值"); System.out.println("Collection -> List"); Collection<String> col = new ArrayList<String>(); col.add("michael"); col.add("xiaoxiao"); List<String> destList = new ArrayList<String>(col); System.out.println("destList size:" + destList.size()); System.out.println("=============================="); |
执行结果:
1 2 3 4 5 6 7 |
============================== List -> Collection destCol size:2 List继承于Collection,可以直接赋值 Collection -> List destList size:2 ============================== |
【五】、 Array Copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
System.out.println("=============================="); System.out.println("Array Copy :String"); String[] srcArr = new String[] { "michael", "xiaoxiao" }; String[] destArr = new String[srcArr.length]; System.arraycopy(srcArr, 0, destArr, 0, srcArr.length); System.out.println("Array Copy : Bean"); TestVo vo1 = new TestVo("michael"); TestVo[] srcObjArr = new TestVo[] { vo1, new TestVo("xiaoxiao") }; TestVo[] destObjArr = new TestVo[srcArr.length]; System.arraycopy(srcObjArr, 0, destObjArr, 0, srcObjArr.length); vo1.setName("michael_new"); System.out.println("srcObjArr[0]的属性name修改为:michael_new"); System.out .println("destObjArr[0]的属性name当前值:" + destObjArr[0].getName()); if (srcObjArr[0] == destObjArr[0]) { System.out.println("Array Copy for Bean : 浅度copy"); } else { System.out.println("Array Copy for Bean : 深度copy"); } System.out.println("=============================="); |
执行结果:
1 2 3 4 5 6 7 |
============================== Array Copy :String Array Copy : Bean srcObjArr[0]的属性name修改为:michael_new destObjArr[0]的属性name当前值:michael_new Array Copy for Bean : 浅度copy ============================== |
【六】、 List Copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
System.out.println("=============================="); System.out.println("List Copy :String"); List<String> srcList = new ArrayList<String>(); srcList.add("michael"); srcList.add("xiaoxiao"); List<String> descList = srcList; List<String> descList2 = new ArrayList<String>(srcList); List<String> descList3 = new ArrayList<String>(Arrays .asList(new String[srcList.size()])); Collections.copy(descList3, srcList); srcList.clear(); System.out.println("源List清空"); System.out.println("直接赋值descList = srcList 的方法copy后的List size: " + descList.size()); System.out.println("new ArrayList<String>(srcList)的方法copy后的List size: " + descList2.size()); System.out.println("Collections.copy(dest,src)的方法copy后的List size: " + descList3.size()); System.out.println("=============================="); |
执行结果:
1 2 3 4 5 6 7 |
============================== List Copy :String 源List清空 直接赋值descList = srcList 的方法copy后的List size: 0 new ArrayList<String>(srcList)的方法copy后的List size: 2 Collections.copy(dest,src)的方法copy后的List size: 2 ============================== |
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/lang/java/array-collection-copy/
0 条评论。