Java中数组、List、Set、Map相互转换

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&lt;String&gt; 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周公解码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值