zoukankan      html  css  js  c++  java
  • List、Set、Map、数组之间各种转换

    刚学Java不久的时候,接到一个电面,然后问了一些java的知识,比如说Java的编码,Unicode等,但是最让我蛋疼的是怎么吗map转为set,那个时候对集合用的很少,对集合不是特别了解,map还知道,set就蒙了,然后转为set更蒙了,觉得应该有API提供吧,但是不知道怎么说。后来我一直下来再查这个问题,查到了,但是没有实践过,今天我就来一发代码。

    List转Set                                                                                   

    Set set = new HashSet(new ArrayList());

    Set转List                                                                                   

    List list = new ArrayList(new HashSet());

    数组转为List                                                                                

    List arr = Arrays.asList("1", "2", "3");
    //或者
    String[] arr = {"1", "2"};
    List list = Arrays.asList(arr);

    数组转为Set                                                                                 

    int[] arr = { 1, 2, 3 };
    Set set = new HashSet(Arrays.asList(arr));

    Map的值转化为List                                                                      

    List list = new ArrayList(map.values());

    Map的值转化为Set                                                                       

    Set set = new HashSet(map.values());

    List转数组                                                                                   

    List list = Arrays.asList("a","b");
    String[] arr = (String[])list.toArray(new String[list.size()]);

    代码                                                                                         

    public class listsetmao {
    
        private static List<String> arrayList;
        private static Map<String, String> hashMap;
        private static Set<String> hashSet;
        private static String[] arr = {"11oneone","22twotwo"};
    
        public static void main(String[] args) {
            /*
             * //list转set initList(); Set<String> set = new
             * HashSet<String>(arrayList);
             * System.out.println("arrayList.toString()--->"+set.toString());
             * System.out.println("set.toString()--->"+set.toString());
             */
            /*
             * //set转list initSet(); List<String> list = new
             * ArrayList<String>(hashSet);
             * System.out.println("hashSet.toString()--->"+hashSet.toString());
             * System.out.println("list.toString()--->"+list.toString());
             */
            /*
            // 数组转为list
            List<String> list = Arrays.asList(arr);
            System.out.println("list.toString()--->"+list.toString());
            */
            /*
            //数组转set
            Set set = new HashSet<>(Arrays.asList(arr));
            System.out.println("set.toString()--->"+set.toString());
            */
            /*
            //map的值转为list
            initMap();
            List<String> list = new ArrayList<String>(hashMap.values());
            System.out.println("list.toString()--->"+list.toString());
            */
            /*
            //map的值转为set
            initMap();
            Set<String> set = new HashSet<String>(hashMap.values());
            System.out.println("set.toString()--->"+set.toString());
            */
            /*
            //map的key转为set
            initMap();
            Set<String> set = new HashSet<String>(hashMap.keySet());
            System.out.println("set.toString()--->"+set.toString());
            */
            //list转数组
            initList();
            String[] arr1 = (String[])arrayList.toArray(new String[arrayList.size()]);
            System.out.println("Arrays.toString(arr1)--->"+Arrays.toString(arr1));
        }
    
        public static void initList() {
            arrayList = new ArrayList<String>();
            arrayList.add("1");
            arrayList.add("2");
            arrayList.add("3");
            arrayList.add("4");
        }
    
        public static void initMap() {
            hashMap = new HashMap<String, String>();
            hashMap.put("one", "one1");
            hashMap.put("two", "two2");
            hashMap.put("three", "three3");
        }
    
        public static void initSet() {
            hashSet = new HashSet<String>();
            hashSet.add("1one");
            hashSet.add("2two");
            hashSet.add("3three");
            hashSet.add("4four");
            hashSet.add("5five");
        }
    
    
    }

    我是天王盖地虎的分割线                                                                 

    源代码:http://pan.baidu.com/s/1dD1Qx01

    listsetmap.zip

    转载请注明出处:http://www.cnblogs.com/yydcdut

  • 相关阅读:
    Demystifying Delegates
    Events
    存储过程与SQL语句的恩怨情仇 转载于-懒人居
    在ASP.NET中实现简单的URL重写
    C#基础概念二十五问
    Asp.Net结合JS在图层上显示记录信息 (转载于海东的技术资料的blog)
    debian 删除SWAP分区后启动报错笔记
    Log4net日志记录开源组件的初级使用
    股票API之新浪财经频道
    .Net Remoting && Web Service && WCF
  • 原文地址:https://www.cnblogs.com/yydcdut/p/3825721.html
Copyright © 2011-2022 走看看