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

[color=red]blog迁移至[/color]:[url=http://www.micmiu.com]http://www.micmiu.com[/url]

周末闲来无事,把java中Array和Collection之间相互转换小结了下,顺便对转换过程中涉及到的javabena的copy深度进行了简单的测试,详细内容将按照如下分类分别介绍:
[color=red][list][*]List <-> Array
[*]Set <-> Array
[*]List <-> Set
[*]List <-> Collection
[*]Array Copy
[*]List Copy[/list][/color]
完整的代码:[url=http://dl.iteye.com/topics/download/dce2e99a-0f99-3b68-b758-d5bf27589578]CollectionConvertTest.zip[/url]
测试过程中用到的javabean:TestVo的代码就不贴了,就一个简单的String属性name.

[color=blue][size=medium]【一】、 List <-> Array[/size][/color]

System.out.println("==============================");

// List -> Array
System.out.println("List -> Array ");
List srclist = new ArrayList();
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 srcVolist = new ArrayList();
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 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 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("==============================");

运行结果:
[quote]
==============================
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
==============================
[/quote]
[color=blue][size=medium]【二】、 Set <-> Array[/size][/color]
 System.out.println("==============================");
// Set -> Array
System.out.println("Set -> Array");
System.out.println("Set -> Array 集合中元素的顺序会变");
Set srcSet = new HashSet();
srcSet.add("michael");
srcSet.add("xiaoxiao");
String[] destArr = srcSet.toArray(new String[srcSet.size()]);
System.out.println(" String destArr size: " + destArr.length);

Set srcObjSet = new HashSet();
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 destSet = new HashSet(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 destObjSet = new HashSet(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("==============================");

执行结果:
[quote]==============================
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
==============================[/quote]
[color=blue][size=medium]【三】、 List <-> Set[/size][/color]
System.out.println("==============================");
// List -> Set
System.out.println("List -> Set");
System.out.println("List -> Set 集合中元素的顺序不变");
List srclist = new ArrayList();
srclist.add("src_list_a");
srclist.add("src_list_b");

Set destSet = new HashSet(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 srcObjList = new ArrayList();
TestVo vo1 = new TestVo("michael");
srcObjList.add(vo1);
srcObjList.add(new TestVo("xiaoxiao"));

Set destObjectSet = new HashSet(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 srcSet = new HashSet();
srcSet.add("src_set_a");
srcSet.add("src_set_b");
List destList = new ArrayList(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 srcObjSet = new HashSet();
TestVo vo3 = new TestVo("vo_3");
srcObjSet.add(vo3);
srcObjSet.add(new TestVo("vo_4"));

List destObjList = new ArrayList(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("==============================");

执行结果:
[quote]==============================
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
==============================[/quote]
[color=blue][size=medium]【四】、 List <-> Collection[/size][/color]
  System.out.println("==============================");
System.out.println("List -> Collection");
List list = new ArrayList();
list.add("michael");
list.add("xiaoxiao");
Collection destCol = list;
System.out.println("destCol size:" + destCol.size());
System.out.println("List继承于Collection,可以直接赋值");

System.out.println("Collection -> List");
Collection col = new ArrayList();
col.add("michael");
col.add("xiaoxiao");
List destList = new ArrayList(col);
System.out.println("destList size:" + destList.size());

System.out.println("==============================");

执行结果:
[quote]==============================
List -> Collection
destCol size:2
List继承于Collection,可以直接赋值
Collection -> List
destList size:2
==============================[/quote]
[color=blue][size=medium]【五】、 Array Copy[/size][/color]
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("==============================");

执行结果:
[quote]==============================
Array Copy :String
Array Copy : Bean
srcObjArr[0]的属性name修改为:michael_new
destObjArr[0]的属性name当前值:michael_new
Array Copy for Bean : 浅度copy
==============================[/quote]
[color=blue][size=medium]【六】、 List Copy[/size][/color]
System.out.println("==============================");
System.out.println("List Copy :String");

List srcList = new ArrayList();
srcList.add("michael");
srcList.add("xiaoxiao");

List descList = srcList;

List descList2 = new ArrayList(srcList);

List descList3 = new ArrayList(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(srcList)的方法copy后的List size: "
+ descList2.size());
System.out.println("Collections.copy(dest,src)的方法copy后的List size: "
+ descList3.size());

System.out.println("==============================");

执行结果:
[quote]==============================
List Copy :String
源List清空
直接赋值descList = srcList 的方法copy后的List size: 0
new ArrayList(srcList)的方法copy后的List size: 2
Collections.copy(dest,src)的方法copy后的List size: 2
==============================[/quote]

转载请注明来自:Michael's blog @[url=http://sjsky.iteye.com/]http://sjsky.iteye.com/[/url]

-----------------------------------分 ------------------------------------隔 ------------------------------------线 --------------------------------------

你可能感兴趣的:(Java)