How would one re-write the following in proper Java 8 functional style using filter, collector, etc:
private BigInteger calculateProduct(char[] letters) {
int OFFSET = 65;
BigInteger[] bigPrimes = Arrays.stream(
new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
97, 101, 103, 107, 109, 113 })
.mapToObj(BigInteger::valueOf)
.toArray(BigInteger[]::new);
BigInteger result = BigInteger.ONE;
for (char c : letters) {
//System.out.println(c+"="+(int)c);
if (c < OFFSET) {
return new BigInteger("-1");
}
int pos = c - OFFSET;
result = result.multiply(bigPrimes[pos]);
}
return result;
}
@Test public void test() {
assertThat(calculateProduct(capitalize("carthorse"))).isEqualTo(calculateProduct(capitalize("orchestra")));
}
private char[] capitalize(String word) {
return word.toUpperCase().toCharArray();
}
You may do it like this:
static final BigInteger[] PRIMES
= IntStream.of(
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113)
.mapToObj(BigInteger::valueOf)
.toArray(BigInteger[]::new);
private BigInteger calculateProduct(char[] letters) {
final int OFFSET = 65;
final CharBuffer cb = CharBuffer.wrap(letters);
if(cb.chars().anyMatch(c -> c<OFFSET))
return BigInteger.ONE.negate();
return cb.chars()
.mapToObj(c -> PRIMES[c-OFFSET])
.reduce(BigInteger.ONE, BigInteger::multiply);
}
Note that moving the creation of the PRIMES
array out of the method, to avoid generating it again for each invocation, is an improvement that works independently of whether you use loops or “functional style” operations.
Also, your code doesn’t handle characters being too large, so you might improve the method to
private BigInteger calculateProduct(char[] letters) {
final int OFFSET = 65;
final CharBuffer cb = CharBuffer.wrap(letters);
if(cb.chars().mapToObj(c -> c-OFFSET).anyMatch(c -> c<0||c>PRIMES.length))
return BigInteger.ONE.negate();
return cb.chars()
.mapToObj(c -> PRIMES[c-OFFSET])
.reduce(BigInteger.ONE, BigInteger::multiply);
}