I have two arraylists A1, A2. Each element of A1 would be the key and each element of A2 the corresponding value. So the solution that I found is looping on A1 ( A1 and A2 have the same size) making hashmap.add(A1[i],A2[i]) but is there a way to directly send the key value pairs as two sets? I want to avoid loops it is going to slow my code..Thank you in advance!
Share
You have a list of key value pairs. The only way to take that list and add it into the
Hashmapis to iterate the list. If you had your key value pairs stored in some other type ofMapobject then you could use theHashmapconstructorHashMap(Map<? extends K,? extends V> m)but not with yourArrayList.Don’t fear the iteration in your code if it is the right approach. Remember Polya: Find a solution, then see if you can find a better solution.