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 strings.

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

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -