Factorial function implementation in Java 8

Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.paradise.function;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
* Created by terrence on 12/12/2016.
*/
public final class FactorialFunction {

public static final Map<Integer, Long> FACTORIAL_MAP = new HashMap<>();

public static final Function<Integer, Long> FACTORIAL = (x) ->
FACTORIAL_MAP.computeIfAbsent(x,
n -> n * FactorialFunction.FACTORIAL.apply(n - 1));

static {
FACTORIAL_MAP.put(1, 1L); // FACTORIAL(1)
}

private FactorialFunction() {

}

}

Unit test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package org.paradise.function;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by terrence on 12/12/2016.
*/
public class FactorialFunctionTest {

@Test
public void testFactorialFunction() throws Exception {

assertEquals("Incorrect result", Long.valueOf(1), FactorialFunction.FACTORIAL.apply(1));
assertEquals("Incorrect result", Long.valueOf(2), FactorialFunction.FACTORIAL.apply(2));

assertEquals("Incorrect result", Long.valueOf(3628800), FactorialFunction.FACTORIAL.apply(10));
}

}