zoukankan      html  css  js  c++  java
  • Map,list,set,集合转化

    public class TestDemo {
        public static void main(String[] args) {
            // set 转list
            List list = new ArrayList(new HashSet());
            // list 转set
            Set set = new HashSet(new ArrayList());
            // 数组转list
            List stooges = Arrays.asList("Larry", "Moe", "Curly");
            // 数组转set
            int[] a = { 1, 2, 3 };
            Set sets = new HashSet(Arrays.asList(a));
            // map的操作
            Map map = new HashMap();
            map.put("1", "a");
            map.put('2', 'b');
            map.put('3', 'c');
            for(Object ma : map.keySet()){
                System.out.printf("ma:"+ma);
            }
            System.out.println(map);
            System.out.println(map.keySet());
            System.out.println(map.values());
            // 将map转为list
            List mapList = new ArrayList(map.values());
            System.out.println(mapList);
            // 将map的值转化为Set
            Set mapSet = new HashSet(map.values());
            System.out.println(mapSet);
            // list转数组
            List listCon = Arrays.asList("a", "b");
            System.out.println(listCon);
            String[] arr = (String[]) list.toArray(new String[list.size()]);
            System.out.println(Arrays.toString(arr));
        }
    }

  • 相关阅读:
    VMware coding Challenge: Coin Toss Betting
    Lintcode: k Sum II
    Lintcode: Interleaving Positive and Negative Numbers
    Lintcode: Insert Node in a Binary Search Tree
    Amazon OA
    Leetcode: Best Time to Buy and Sell Stock IV
    Leetcode: Reverse Words in a String II
    Leetcode: Repeated DNA Sequence
    Leetcode: Rotate Array
    VMware coding Challenge:Date of Weekday
  • 原文地址:https://www.cnblogs.com/love-you-girl/p/3811619.html
Copyright © 2011-2022 走看看