I have a complex loop that is looking for matches. primeFactors List. Its nth element contains a sorted list of simple decompositions of n. I check if c and d checkIfPrimes using checkIfPrimes
boolean checkIfPrimes(int c, int d, List<List<Integer>> primeFactors) { List<Integer> common = new ArrayList<>(primeFactors.get(d));
primeFactors.get(d).retainAll(primeFactors.get(c)) looks promising, but it will change my reusable primeFactors object.
Creating a new object is relatively slow. Is there a way to speed up this step? Can I somehow make use of the fact that the lists are sorted? Should I use arrays instead?
java performance new-operator
sixtytrees
source share