zoukankan      html  css  js  c++  java
  • java集合的互转

    List<-->数组、List<-->Set、数组<-->Set、Map将键转化为Set、Map将值转化为Set、Map将值转化为List等集合常

    public class Test {
    
        public static void main(String[] args) {
            //List-->数组
            List<String> list = new ArrayList<String>();
            list.add("Tom");
            list.add("Java");
            list.add("Jack");
            Object[] objects = list.toArray();//返回Object数组
            System.out.println("objects:"+Arrays.toString(objects));
            String[] strings1 = new String[list.size()];
            list.toArray(strings1);//将转化后的数组放入已经创建好的对象中
            System.out.println("strings1:"+Arrays.toString(strings1));
            String[] strings2 = list.toArray(new String[0]);//将转化后的数组赋给新对象
            System.out.println("strings2:"+Arrays.toString(strings2));
            //数组-->List
            String[] ss = {"Jack","Kitter"};
            List<String> list1 = Arrays.asList(ss);
            List<String> list2 = Arrays.asList("Tom","Jack");
            System.out.println(list1);
            System.out.println(list2);
            //Set --> List
            List<String> list3 = new ArrayList<String>(new HashSet<String>()); 
            //List --> Set
            Set<String> set = new HashSet<String>(new ArrayList<String>());
            //数组-->Set
            String[] strs = {"AA","BB"};
            Set<String> set2 = new HashSet<String>(Arrays.asList(strs));
            System.out.println(set2);
            //Set-->数组
            Set<String> set3 = new HashSet<String>(Arrays.asList("PP","OO"));
            String[] strSet = new String[set3.size()];
            set3.toArray(strSet);
            System.out.println(Arrays.toString(strSet));
            //Map操作
            Map<String, String> map = new HashMap<String, String>();
            map.put("YYY", "UUU");
            map.put("RRR", "TTT");
            // 将键转化为Set  
            Set<String> mapKeySet = map.keySet();
            // 将值转化为Set  
            Set<String> mapValuesSet = new HashSet<String>(map.values());
            // 将值转化为List  
            List<String> mapValuesList = new ArrayList<String>(map.values());
    
        }
    }
  • 相关阅读:
    C#与JS实现 获取指定字节长度 中英文混合字符串 的方法
    CKFinder_AspDotNet_2.4 破解方法 去版权
    MVC3中如何输出富文本
    CKEditor与CKFinder整合 MVC3
    vue项目多页配置
    设备宽度
    h5前端项目常见问题汇总
    react分享
    javascript-复制
    vue开发笔记
  • 原文地址:https://www.cnblogs.com/andy-do/p/3141640.html
Copyright © 2011-2022 走看看