LIST android 数据到后台 android list转数组 |
您所在的位置:网站首页 › list强转set › LIST android 数据到后台 android list转数组 |
★ 数组转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);CollectionUtils.addAll()方法的实现很简单,只是循环使用了Collection的add()方法而已。如果只是想将数组转换成List,可以用JDK中的java.util.Arrays类:import java.util.Arrays; String[] strArray = {"aaa", "bbb", "ccc"}; List strList = Arrays.asList(strArray);不过Arrays.asList()方法返回的List不能add对象,因为该方法的实现是使用参数引用的数组的大小来new的一个ArrayList。 ★ Collection转数组直接使用Collection的toArray()方法,该方法有两个重载版本: Object[] toArray(); T[] toArray(T[] a);★ Map转Collection 直接使用Map的values()方法。 ★ List和Set转换 List list = new ArrayList(new Hashset());// Fixed-size list List list = Arrays.asList(array);// Growable list list = new LinkedList(Arrays.asList(array));// Duplicate elements are discarded Set set = new HashSet(Arrays.asList(array));String[] strArray = {"aaa", "bbb", "ccc"}; List strList = new ArrayList(); Collections.addAll(strList, strArray); strList.add("aaa"); strList.add("bbb"); Set set = new HashSet(strList); Log.e(" |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |