Convert List<Long> to Map<Long, Long> that counts occurrences
If you want to group the elements, you have to use groupingBy
:
import static java.util.stream.Collectors.*;
Map<Long, Long> primeFactorCount = primeFactors.stream()
.collect(groupingBy(p -> p, counting()));
If you use Eclipse Collections, you could use the following for the prime factors list and the prime factor count. A Bag
is basically a Map<K, Integer>
.
MutableList<Long> primeFactors = this.primeFactors(factorProduct);
Bag<Long> primeFactorCount = primeFactors.toBag();
Use a FastList
in the primeFactors method above.
In the case of Eclipse Collections we have primitive Lists and Bags, so you will not need to box any results.
LongList primeFactors = this.primeFactors(factorProduct);
LongBag primeFactorCount = primeFactors.toBag();
Use a LongArrayList
instead in the primeFactors method above.
Note: I am committer for Eclipse Collections.