I have a tight loop that searches coprimes. A list
primeFactors
c
d
checkIfPrimes
boolean checkIfPrimes(int c, int d, List<List<Integer>> primeFactors) {
List<Integer> common = new ArrayList<>(primeFactors.get(d)); //slow
common.retainAll(primeFactors.get(c));
return (common.isEmpty());
}
primeFactors.get(d).retainAll(primeFactors.get(c))
primeFactors
You could do something along the lines of:
List<Integer> commonElements =
primeFactors.get(d).stream()
.filter(primeFactors.get(c)::contains)
.collect(Collectors.toList());
Once you measure this performance, you can substitute 'parallelStream()' for 'stream()' above and see what benefits you derive.