I want to perform this part of my code because previously I had 3 IF for corresponding Match like
if (matcher1.find()) {
myMap.put(matcher1.group(1), matcher1.group(2)
}
if (matcher2.find()) {
myMap.put(matcher2.group(1), matcher2.group(2)
}
if (matcher3.find()) {
myMap.put(matcher3.group(1), matcher3.group(2)
}
and I want to know if I can englobe this 3 match in one if and put on my map with the corresponding matcher :) like :
for (int i = 0; i < result.size(); i++) {
Matcher matcher1= patternRecordType.matcher(result.get(i));
Matcher matcher2 = patternCustomerAreaRecordType.matcher(result.get(i));
Matcher match3 = patternTotal.matcher(result.get(i));
if (matcher1.find() || matcher2.find() || matcher3.find()) {
myMap.put(matcherX.group(1), matcherX.group(2));
}
}
It is not possible to replace your 3 "ifs" with a single one. Note that in the first case, in a single interation you can enter into 3,2,1 or 0 "if" blocks resulting in having 0-3 elements added to the map. If you merge all 3 conditions into a single "if" you will enter the "if" only one at best, resulting at at most 1 added element into the map.
However, you could create a collection of all the Matchers and then perform matching and adding for each of them. It would look something like this:
List<Matcher> matchers = new ArrayList<>();
Matcher matcher1= patternRecordType.matcher(result.get(i));
Matcher matcher2 = patternCustomerAreaRecordType.matcher(result.get(i));
Matcher matcher3 = patternTotal.matcher(result.get(i));
matchers.add(matcher1);
matchers.add(matcher2);
matchers.add(matcher3);
for (Matcher matcher : matchers){
if (matcher.find()) {
myMap.put(matcher.group(1), matcher.group(2));
}
}
For only 3 Matchers it would be overcomplicating the code but if you have over 5 different Matchers then I would consider something like this.