java - How can I convert a HashMap<String, ArrayList<String>> to a HashMap<String, String[]>? -
i have hashmap<string, arraylist<string>>
. trying convert hashmap<string, string[]>
.
hashmap<string, arraylist<string>> arraylistmap = new hashmap<>(); hashmap<string, string[]> arraymap = new hashmap<>(); (map.entry<string, arraylist<string>> entry : arraylistmap.entryset()) { arraymap.put(entry.getkey(), entry.getvalue().toarray()); }
however, entry.getvalue().toarray()
, ide giving me error:
wrong 2nd argument type. found: 'java.lang.object[], required 'java.lang.string[]'.
i don't know why, because arraylistmap
specifies working string
s.
why not working, , how can fix it?
arraylist
has overloaded toarray
method.
the first form, toarray()
, return object[]
back. isn't want, since can't convert object[]
string[]
.
the second form, toarray(t[] a)
return array typed whatever array pass it.
you need use second form here array correctly typed.
arraymap.put(entry.getkey(), entry.getvalue() .toarray(new string[entry.getvalue().size()]));
Comments
Post a Comment