Java中数组、List、Set、Map相互转换总结:
创建一个数组:
String[] strArray = new String[]{"Tom", "Bob", "Jane"};
0、数组—>单列集合
可以调用类CollectionUtils中的addAll()方法来转换:
List<Object> arrayList = new ArrayList<>();
CollectionUtils.addAll(arrayList, strArray);
Set<Object> hashSet = new HashSet<>(Arrays.asList(strArray));
CollectionUtils.addAll(hashSet, strArray);
查看源码中它的实现方法:
/**
* Adds all elements in the array to the given collection.
*
* @param collection the collection to add to, must not be null
* @param elements the array of elements to add, must not be null
* @throws NullPointerException if the collection or array is null
*/
public static void addAll(Collection collection, Object[] elements) {
for (int i = 0, size = elements.length; i < size; i++) {
collection.add(elements[i]);
}
}
源码中说:添加数组中所有的元素到Collection集合中,具体的实现方法就是通过for循环添加数组中的元素。
这种方法我们使用的比较少,毕竟大多数是直接转化成我们具体使用的集合类型List或者Set。
1、数组—>List
数组转化成List集合使用数组中提供的asList方法:
List<String> asList = Arrays.asList(strArray);
但是我们在转化后的集合中对元素进行操作时要注意:
asList.add("abc");
asList.remove(0);
会报错:UnsupportedOperationException
看源码就能知道原因:
/**
* Returns a fixed-size list backed by the specified array. (Changes to
* the returned list "write through" to the array.) This method acts
* as bridge between array-based and collection-based APIs, in
* combination with {@link Collection#toArray}. The returned list is
* serializable and implements {@link RandomAccess}.
*
* <p>This method also provides a convenient way to create a fixed-size
* list initialized to contain several elements:
* <pre>
* List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
* </pre>
*
* @param <T> the class of the objects in the array
* @param a the array by which the list will be backed
* @return a list view of the specified array
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
注释第一句话就是:返回指定数组支持的固定大小列表。
由于转化后list的size固定,所以不能进行添加、删除的操作。
如果我们将数组转化成集合后还需要对集合中的元素增删,可以创建新的集合,将数组元素循环添加到集合中,如:
List<Object> arrayList = new ArrayList<>();
for(String s : strArray) {
arrayList.add(s);
}
arrayList.add("abc");
arrayList.remove(0);
2、数组—>Set
将数组转换成List后再转换成Set:
Set<Object> hashSet = new HashSet<>(Arrays.asList(strArray));
hashSet.add("abc");
hashSet.add("edf");
3、List—>数组
使用集合中的toArray()方法
List<Object> list = new ArrayList<>();
list.add("ab");
list.add("cd");
list.add("ef");
Object[] array = list.toArray();
4、List—>Set
HashSet<Object> hashSet = new HashSet<>(list);
hashSet.add("aa");
hashSet.add("bb");
5、Set—>数组
Set<Object> hashSet = new HashSet<>();
hashSet.add("aa");
hashSet.add("bb");
Object[] array = hashSet.toArray();
6、Set—>List
List<Object> arrayList = new ArrayList<>(hashSet);
arrayList.add("cc");
arrayList.add("dd");
7、双列集合Map中的Key和Value转化为单列集合List、Set
Map<Object,Object> map = new HashMap<>();
map.put("a", "abc");
map.put("b", "bcd");
map.put("c", "cde");
我们可以看到k、v的数据结构分别是:
Set<Object> keySet = map.keySet();
Collection<Object> values = map.values();
或许这就是Map中的键保证唯一的原因之一。
Map中的key转化成list和set:
List<Object> arrayList2 = new ArrayList<>(keySet);
List<Object> arrayList3 = new ArrayList<>(values);
Map中的value转化成list和set:
Set<Object> hashSet2 = new HashSet<>(keySet);
Set<Object> hashSet3 = new HashSet<>(values);
参考文章:
https://blog.csdn.net/top_code/article/details/10552827
https://blog.csdn.net/my_precious/article/details/53010232
https://www.cnblogs.com/ocean2017/p/7078125.html