★ 数组转Collection(浅转换)
用JDK中的java.util.Arrays类
import java.util.Arrays;
String[] strArray = {"aaa", "bbb", "ccc"};
List strList = Arrays.asList(strArray);
不过Arrays.asList()方法返回的List不能add,remove对象,因为该方法的实现是使用参数引用的数组的大小来new的一个ArrayList,对返回列表的更改会“直写”到数组,而数组没有add,remove方法。
★ 数组转Collection(深转换)
使用Apache Jakarta Commons Collections包
import org.apache.commons.collections.CollectionUtils;
String[] strArray = {"aaa", "bbb", "ccc"};
List strList = new ArrayList();
Set strSet = new HashSet();
CollectionUtils.addAll(strList, strArray);
CollectionUtils.addAll(strSet, strArray);
将数组完全转换成一个任意类型的Collection
用JDK中的java.util.Arrays类
import java.util.Arrays;
String[] strArray = {"aaa", "bbb", "ccc"};
List strList = Arrays.asList(strArray);
不过Arrays.asList()方法返回的List不能add,remove对象,因为该方法的实现是使用参数引用的数组的大小来new的一个ArrayList,对返回列表的更改会“直写”到数组,而数组没有add,remove方法。
★ 数组转Collection(深转换)
使用Apache Jakarta Commons Collections包
import org.apache.commons.collections.CollectionUtils;
String[] strArray = {"aaa", "bbb", "ccc"};
List strList = new ArrayList();
Set strSet = new HashSet();
CollectionUtils.addAll(strList, strArray);
CollectionUtils.addAll(strSet, strArray);
将数组完全转换成一个任意类型的Collection