zoukankan      html  css  js  c++  java
  • java中List、Array、Map、Set等集合相互转

     

    1.List转换为Array

    List<String> list = new ArrayList<>();

    list.add("AAAA");

    list.add("BBBB");

    list.add("CCCC");

    list.add("DDDD");

    String [] array = list.toArray(new String[list.size()]);

    2.

    Array转换为List

    String[] countries = {"AAAA", "BBBB", "CCCC", "DDDD"};

    List list = Arrays.asList(countries);

    3.

    Map的Key值转换为List

    Map<Integer,String> map = new HashMap<>();

    map.put(1,"AAAA");

    map.put(2,"BBBB");

    map.put(3,"CCCC");

    map.put(4,"DDDD");

    List<Integer> list = new ArrayList<Integer>(map.keySet());

    4.

    Map的Value值转换为List

    Map<Integer,String> map = new HashMap<>();

    map.put(1,"AAAA");

    map.put(2,"BBBB");

    map.put(3,"CCCC");

    map.put(4,"DDDD");

    List<String> list = new ArrayList<String>(map.values());

    5.

    Array转换为Set

    String [] countries = {"AAAA", "BBBB", "CCCC", "DDDD"};

    Set<String> set = new HashSet<String>(Arrays.asList(countries));

    注:如果Array中存在相同的值,Set中只会存在一个

    6.

    Map的Key值转换为Set

    Map<Integer,String> map = new HashMap<>();

    map.put(1,"AAAA");

    map.put(2,"BBBB");

    map.put(3,"CCCC");

    map.put(4,"DDDD");

    Set<Integer> set = new HashSet<>(map.keySet());

    7.

    Map的Value值转换为Set

    Map<Integer,String> map = new HashMap<>();

    map.put(1,"AAAA");

    map.put(2,"BBBB");

    map.put(3,"CCCC");

    map.put(4,"DDDD");

    Set<String> set = new HashSet<String>(map.values());

  • 相关阅读:
    mac 卸载 node并重新安装
    最小的Django应用
    Python如何实现文本转语音
    Python语言库pyttsx3
    大数据资料
    剑指offer(29)最小的K个数
    剑指offer(28)数组中出现次数超过一半的数
    剑指offer(27)字符串的排列
    剑指offer(26)二叉搜索树与双向链表
    JS、JAVA刷题和C刷题的一个很重要的区别
  • 原文地址:https://www.cnblogs.com/l1205/p/14134876.html
Copyright © 2011-2022 走看看