import java.util.Map; import java.util.HashMap; public class MemoizationUsingMap { static Map mzTable; static long mzFib(int n) { if (!mzTable.containsKey(n)) { long value = (n < 2) ? n : mzFib(n-1) + mzFib(n-2); mzTable.put(n, value); } return mzTable.get(n); } public static void main(String[] args) { int n = Integer.parseInt(args[0]); if (n < 0) { System.err.println("argument must be a nonnegative integer"); System.exit(1); } long value; double time; Stopwatch sw = new Stopwatch(); mzTable = new HashMap(); value = mzFib(n); time = sw.elapsedTime(); System.out.printf("%nmzFib(%d) = %d%n", n, value); System.out.printf("time spent: %.2f%n", time); } }