Java: how to convert HashMap to array

前端 未结 12 1907
时光取名叫无心
时光取名叫无心 2020-11-29 16:36

I need to convert a HashMap to an array; could anyone show me how it\'s done?

相关标签:
12条回答
  • 2020-11-29 16:54

    If you have HashMap<String, SomeObject> hashMap then:

    hashMap.values().toArray();
    

    Will return an Object[]. If instead you want an array of the type SomeObject, you could use:

    hashMap.values().toArray(new SomeObject[0]);
    
    0 讨论(0)
  • 2020-11-29 16:57

    I used almost the same as @kmccoy, but instead of a keySet() I did this

    hashMap.values().toArray(new MyObject[0]);
    
    0 讨论(0)
  • 2020-11-29 17:01
    hashMap.keySet().toArray(); // returns an array of keys
    hashMap.values().toArray(); // returns an array of values
    

    Edit

    It should be noted that the ordering of both arrays may not be the same, See oxbow_lakes answer for a better approach for iteration when the pair key/values are needed.

    0 讨论(0)
  • 2020-11-29 17:01
    @SuppressWarnings("unchecked")
        public static <E,T> E[] hashMapKeysToArray(HashMap<E,T> map)
        {
            int s;
            if(map == null || (s = map.size())<1)
                return null;
    
            E[] temp;
            E typeHelper;
            try
            {
                Iterator<Entry<E, T>> iterator = map.entrySet().iterator();
                Entry<E, T> iK = iterator.next();
                typeHelper = iK.getKey();
    
                Object o = Array.newInstance(typeHelper.getClass(), s);
                temp = (E[]) o;
    
                int index = 0;
                for (Map.Entry<E,T> mapEntry : map.entrySet())
                {
                    temp[index++] = mapEntry.getKey();
                }
            }
            catch (Exception e)
            {
                return null;
            }
            return temp;
        }
    //--------------------------------------------------------
        @SuppressWarnings("unchecked")
        public static <E,T> T[] hashMapValuesToArray(HashMap<E,T> map)
        {
            int s;
            if(map == null || (s = map.size())<1)
                return null;
    
            T[] temp;
            T typeHelper;
            try
            {
                Iterator<Entry<E, T>> iterator = map.entrySet().iterator();
                Entry<E, T> iK = iterator.next();
                typeHelper = iK.getValue();
    
                Object o = Array.newInstance(typeHelper.getClass(), s);
                temp = (T[]) o;
    
                int index = 0;
                for (Map.Entry<E,T> mapEntry : map.entrySet())
                {
                    temp[index++] = mapEntry.getValue();
                }
            }
            catch (Exception e)
            {return null;}
    
            return temp;
        }
    
    0 讨论(0)
  • 2020-11-29 17:04

    If you are using Java 8+ and need a 2 dimensional Array, perhaps for TestNG data providers, you can try:

    map.entrySet()
        .stream()
        .map(e -> new Object[]{e.getKey(), e.getValue()})
        .toArray(Object[][]::new);
    

    If your Objects are Strings and you need a String[][], try:

    map.entrySet()
        .stream()
        .map(e -> new String[]{e.getKey(), e.getValue().toString()})
        .toArray(String[][]::new);
    
    0 讨论(0)
  • 2020-11-29 17:05

    To guarantee the correct order for each array of Keys and Values, use this (the other answers use individual Sets which offer no guarantee as to order.

    Map<String, Object> map = new HashMap<String, Object>();
    String[] keys = new String[map.size()];
    Object[] values = new Object[map.size()];
    int index = 0;
    for (Map.Entry<String, Object> mapEntry : map.entrySet()) {
        keys[index] = mapEntry.getKey();
        values[index] = mapEntry.getValue();
        index++;
    }
    
    0 讨论(0)
提交回复
热议问题