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( "==============================" ); |
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" ); |
9 |
String[] destArr = srclist.toArray( new String[srclist.size()]); |
10 |
System.out.println( "List -> Array 集合中元素的顺序不变" ); |
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" ); |
17 |
System.out.println( "List -> Array for String : 浅度copy" ); |
20 |
List<TestVo> srcVolist = new ArrayList<TestVo>(); |
21 |
TestVo vo1 = new TestVo( "michael" ); |
23 |
srcVolist.add( new TestVo( "xiaoxiao" )); |
24 |
srcVolist.add( new TestVo( "vo_3" )); |
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" ); |
34 |
System.out.println( "List -> Array for Bean : 深度copy" ); |
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" ); |
45 |
System.out.println( "Array -> List for String : 浅度copy" ); |
48 |
TestVo vo3 = new TestVo( "vo_3" ); |
49 |
TestVo[] srcObjArr = new TestVo[] { vo3, new TestVo( "vo_4" ) }; |
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" ); |
59 |
System.out.println( "Array -> List for Bean : 深度copy" ); |
61 |
System.out.println( "==============================" ); |
运行结果:
1 |
============================== |
3 |
List -> Array 集合中元素的顺序不变 |
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 |
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( "==============================" ); |
3 |
System.out.println( "Set -> Array" ); |
4 |
System.out.println( "Set -> Array 集合中元素的顺序会变" ); |
5 |
Set<String> srcSet = new HashSet<String>(); |
7 |
srcSet.add( "xiaoxiao" ); |
8 |
String[] destArr = srcSet.toArray( new String[srcSet.size()]); |
9 |
System.out.println( " String destArr size: " + destArr.length); |
11 |
Set<TestVo> srcObjSet = new HashSet<TestVo>(); |
12 |
TestVo vo1 = new TestVo( "michael" ); |
14 |
srcObjSet.add( new TestVo( "xiaoxiao" )); |
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())) { |
27 |
System.out.println( "Set -> Array for Bean : 深度copy" ); |
29 |
System.out.println( "Set -> Array for Bean : 浅度copy" ); |
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)); |
38 |
System.out.println( " String destSet size: " + destSet.size()); |
40 |
TestVo vo3 = new TestVo( "michael" ); |
41 |
TestVo[] srcObjArr = new TestVo[] { vo1, new TestVo( "xiaoxiao" ) }; |
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()); |
47 |
for (TestVo vo : destObjSet) { |
48 |
if ( "michael_new" .equals(vo.getName())) { |
54 |
System.out.println( "Array -> Set for Bean : 深度copy" ); |
56 |
System.out.println( "Array -> Set for Bean : 浅度copy" ); |
59 |
System.out.println( "==============================" ); |
执行结果:
1 |
============================== |
3 |
Set -> Array 集合中元素的顺序会变 |
6 |
Set -> Array for Bean : 浅度copy |
8 |
Array -> Set 集合中元素的顺序会变 |
11 |
Array -> Set for Bean : 浅度copy |
12 |
============================== |
【三】、 List <-> Set
1 |
System.out.println( "==============================" ); |
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" ); |
9 |
Set<String> destSet = new HashSet<String>(srclist); |
11 |
if (destSet.size() == 2 ) { |
12 |
System.out.println( "List -> Set for String : 深度copy" ); |
14 |
System.out.println( "List -> Set for String : 浅度copy" ); |
17 |
List<TestVo> srcObjList = new ArrayList<TestVo>(); |
18 |
TestVo vo1 = new TestVo( "michael" ); |
20 |
srcObjList.add( new TestVo( "xiaoxiao" )); |
22 |
Set<TestVo> destObjectSet = new HashSet<TestVo>(srcObjList); |
23 |
vo1.setName( "michael_new" ); |
25 |
if ( "michael_new" .equals(destObjectSet.iterator().next().getName())) { |
26 |
System.out.println( "List -> Set for Bean : 浅度copy" ); |
28 |
System.out.println( "List -> Set for Bean : 深度copy" ); |
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); |
39 |
if (destList.size() == 2 ) { |
40 |
System.out.println( "Set -> List for String : 深度copy" ); |
42 |
System.out.println( "Set -> List for String : 浅度copy" ); |
45 |
Set<TestVo> srcObjSet = new HashSet<TestVo>(); |
46 |
TestVo vo3 = new TestVo( "vo_3" ); |
48 |
srcObjSet.add( new TestVo( "vo_4" )); |
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" ); |
56 |
System.out.println( "Set -> List for Bean : 深度copy" ); |
58 |
System.out.println( "==============================" ); |
执行结果:
1 |
============================== |
4 |
List -> Set for String : 深度copy |
5 |
List -> Set for Bean : 浅度copy |
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>(); |
6 |
Collection<String> destCol = list; |
7 |
System.out.println( "destCol size:" + destCol.size()); |
8 |
System.out.println( "List继承于Collection,可以直接赋值" ); |
10 |
System.out.println( "Collection -> List" ); |
11 |
Collection<String> col = new ArrayList<String>(); |
14 |
List<String> destList = new ArrayList<String>(col); |
15 |
System.out.println( "destList size:" + destList.size()); |
17 |
System.out.println( "==============================" ); |
执行结果:
1 |
============================== |
4 |
List继承于Collection,可以直接赋值 |
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]; |
6 |
System.arraycopy(srcArr, 0 , destArr, 0 , srcArr.length); |
8 |
System.out.println( "Array Copy : Bean" ); |
10 |
TestVo vo1 = new TestVo( "michael" ); |
11 |
TestVo[] srcObjArr = new TestVo[] { vo1, new TestVo( "xiaoxiao" ) }; |
12 |
TestVo[] destObjArr = new TestVo[srcArr.length]; |
14 |
System.arraycopy(srcObjArr, 0 , destObjArr, 0 , srcObjArr.length); |
15 |
vo1.setName( "michael_new" ); |
16 |
System.out.println( "srcObjArr[0]的属性name修改为:michael_new" ); |
18 |
.println( "destObjArr[0]的属性name当前值:" + destObjArr[ 0 ].getName()); |
19 |
if (srcObjArr[ 0 ] == destObjArr[ 0 ]) { |
20 |
System.out.println( "Array Copy for Bean : 浅度copy" ); |
22 |
System.out.println( "Array Copy for Bean : 深度copy" ); |
24 |
System.out.println( "==============================" ); |
执行结果:
1 |
============================== |
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" ); |
4 |
List<String> srcList = new ArrayList<String>(); |
5 |
srcList.add( "michael" ); |
6 |
srcList.add( "xiaoxiao" ); |
8 |
List<String> descList = srcList; |
10 |
List<String> descList2 = new ArrayList<String>(srcList); |
12 |
List<String> descList3 = new ArrayList<String>(Arrays |
13 |
.asList( new String[srcList.size()])); |
14 |
Collections.copy(descList3, srcList); |
17 |
System.out.println( "源List清空" ); |
18 |
System.out.println( "直接赋值descList = srcList 的方法copy后的List size: " |
20 |
System.out.println( "new ArrayList<String>(srcList)的方法copy后的List size: " |
22 |
System.out.println( "Collections.copy(dest,src)的方法copy后的List size: " |
25 |
System.out.println( "==============================" ); |
执行结果:
1 |
============================== |
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 |
============================== |