一、java.util包数据结构互相转换
这里讨论的为collection接口的实现类(ArrayList、LinkedList、Stack、Vector、HashSet、TreeSet)以及数组之间的转化。
二、结论
1、collection接口的实现类(ArrayList、LinkedList、Stack、Vector、HashSet、TreeSet)之间都是可以两两之间互相转换的。它们大多都是通过向构造函数中传入其它数据结构实例实现的。只有stack比较特殊,只能通过先创建空的stack,再通过addAll方法添加其它数据结构的元素。
2、数组转换为collection接口的实现类实例(ArrayList、LinkedList、Stack、Vector、HashSet、TreeSet)需要借助第三方类库org.apache.commons.collections4.CollectionUtils实现。
3、collection接口的实现类实例(ArrayList、LinkedList、Stack、Vector、HashSet、TreeSet)转换为数组只需要借助它们自己的实例方法T[] toArray(T[] a);
即可。
三、实验代码
import org.apache.commons.collections4.CollectionUtils;
public class Test {
public static void main(String[] args) {
/**
* 数据准备
* */
System.out.println("origin data:");
List<String> arrayList = new ArrayList<String>();
arrayList.add("listElement1");
arrayList.add("listElement4");
arrayList.add("listElement3");
System.out.println("arrayList ele: " + arrayList);
Set<String> hashSet = new HashSet<String>();
hashSet.add("setElement1");
hashSet.add("setElement2");
hashSet.add("setElement3");
System.out.println("hashset ele: " + hashSet);
System.out.println();
/**
* 转换set为各List接口子类(ArrayList、LinkedList、Stack、Vector)
* */
System.out.println("create list related from set:");
List<String> arrayList2 = new ArrayList<String>(hashSet);
System.out.println("arraylist2FromHashset ele: " + arrayList2);
Vector<String> vector1 = new Vector<String>(hashSet);
System.out.println("vectorFromHashset ele: " + vector1);
// Stack<String> stack = new Stack<String>(set);//报错,没有该构造方法
Stack<String> stack1 = new Stack<String>();
stack1.addAll(hashSet);
System.out.println("stackFromHashset ele: " + stack1);
System.out.println();
/**
* 各List接口子类(ArrayList、LinkedList、Stack、Vector)之间互相转换
* */
System.out.println("create list related create from each other");
ArrayList<String> arrayList3 = new ArrayList<String>(vector1);
System.out.println("arraylist3FromVector: " + arrayList3);
Vector<String> vector2 = new Vector<String>(arrayList3);
System.out.println("vector2FromArrayList: " + vector2);
Stack<String> stack2 = new Stack<String>();
stack2.addAll(arrayList3);
stack2.addAll(vector2);
System.out.println("stack2FromArrayList: " + stack2);
LinkedList<String> linkedList = new LinkedList<String>(arrayList3);
System.out.println("linkedListFromArrayList: " + linkedList);
ArrayList<String> arrayList4 = new ArrayList<String>(linkedList);
System.out.println("arrayList4FromLinkedList: " + arrayList4);
/**
* 通过各List接口子类(ArrayList、LinkedList、Stack、Vector)转换为set
* */
System.out.println("create set related from list:");
Set<String> set2 = new HashSet<String>(arrayList);
System.out.println("hashset2FromArrayList ele: " + set2);
System.out.println();
/**
* set接口子类互相转换
* */
TreeSet<String> set3 = new TreeSet<String>(set2);
System.out.println("treeset3FromHashset: " + set3);
HashSet<String> set4 = new HashSet<String>(set3);
System.out.println("hashset4FromTreeSet: " + set4);
/**
* 数组转换为collect各子类
* 需要通过第三方类库
* */
Boolean [] arr = new Boolean[]{true,false,false};
List<Boolean> arraylistFromArr = new ArrayList<Boolean>();
CollectionUtils.addAll(arraylistFromArr, arr);
System.out.println("arraylistFromArr: " + arraylistFromArr);
/**
* collection各子类转换为数组
* */
String [] arr1 = arrayList.toArray(new String[0]);
printArray(arr1);
arr1 = linkedList.toArray(new String[0]);
printArray(arr1);
arr1 = vector1.toArray(new String[0]);
printArray(arr1);
arr1 = stack1.toArray(new String[0]);
printArray(arr1);
arr1 = hashSet.toArray(new String[0]);
printArray(arr1);
arr1 = set3.toArray(new String[0]);
printArray(arr1);
}
private static void printArray(String [] arr){
int count = 0;
for(String s : arr){
System.out.print(s + " ");
count++;
if(count == arr.length){
System.out.println();
}
}
}
}
四、参考
http://blog.csdn.net/courage89/article/details/7877722
http://www.cnblogs.com/huangfox/archive/2012/07/05/2577306.html