Best way to convert a Java Map to a Dictionary in C#

c#, dictionary, ikvm

Solution

You can create an extension method for `map to Dictionary`

java.util.Map map = new java.util.HashMap();
map.put("a", 1);
map.put("b", 2);

var dict = map.ToDictionary<string, int>();
public static class JavaUtils
{
    public static Dictionary<K, V> ToDictionary<K, V>(this java.util.Map map)
    {
        var dict = new Dictionary<K, V>();
        var iterator = map.keySet().iterator();
        while (iterator.hasNext())
        {
            var key = (K)iterator.next();
            dict.Add(key, (V)map.get(key));
        }
        return dict;
    }
}

Problem

I need to write a function in C# that takes in a java.util.Map and converts it to a C# Dictionary. (The project I'm working on uses IKVM to use some Java classes in C#.) I've tried using a foreach loop (e.g. `foreach (java.util.Map.Entry in map)` or `foreach (string key in map.keySet())` to add to the Dictionary element-by-element, but it seems that Java Maps and Sets are not enumerable. What's my best approach here? Should I use a java.util.Iterator? (I'm not opposed to using a Java Iterator on principle, but it feels like there should be a cleaner, more "c-sharp-y" way.) I guess I could get the keySet, use the Set method toArray(), and iterate through that, but again, it doesn't feel "c-sharp-y". Should I just get over myself and do one of those things, or is there a better way? (Or, of those two options, which is more efficient in terms of time/space taken?) For reference, here's a sketch of what I'm trying to do: ``` public Dictionary<string, object> convertMap(java.util.Map map) { Dictionary<string, object> dict = new Dictionary<string, object>(); foreach (String key in map.keySet()) // doesn't work; map is not enumerable dict.Add(key, map.get(key)); return dict; } ```

Original source