I have a very large lists, so I need to speed up the whole, I'm trying to parallelize this for loop:
public HashMap<String, String> getData()
{
//Both list are ArrayList<String>
HashMap<String, String> hashMap = new HashMap<>();
for (int w = 0; w < firstList.size(); w++) {
boolean once = false;
for (int j = 0; j < secondList.size(); j++) {
if (!once && secondList.get(j).var.startsWith(firstList.get(w).var.toLowerCase())) {
hashMap.put(firstList.get(w).var, secondList.get(j).var);
once = true;
}
}
}
return hashMap;
}
I've found this good answer Parallelizing a for loop, but not really understand how to apply it to my case, I should create two Callable <output>
for <K, V>
of my HashMap
?
Or am I doing wrong to use this method ?
The problem is not how to parallelize the loop. You are using the wrong approach.
If I understand correctly you want for each element of list 1 to add in a hashmap 1 entry from list 2 that starts with the same string.
First of all I don't understand why you don't break out of the loop when you find a match and you use the once
variable.
Also why do you need once
variable since you can check if the word of list1 already exists in the hashmap?
Anyway you should be using a TreeMap (check NavigableMap interface) instead of a hashmap which checks for close matches.
Also why can't you do this logic when creating the lists in the first place?
Perhaps you are trying to optimise the wrong thing?