If you’re doing programming for a while or simply began with programming there’s a good probability that you simply heard concerning the Fibonacci collection. It is one of many primary programming workout routines which many people used to be taught Programming.
That is okay if it’s a must to simply print 100 Fibonacci quantity and you might not discover a distinction but when it’s a must to print 1 million numbers then this turns into a giant downside and it might take hours to print the collection however when you use the cache, you may drastically minimize down the execution time.
In an effort to construct the cache, we’ll use the newly added computeIfAbsetnt() methodology from the Map interface to calculate a brand new worth provided that we don’t have already got a price for a given key. On condition that this methodology is assured to execute atomically, and since we’re utilizing a ConcurrentHashMap, this cache is even thread-safe.
Find out how to Print Fibonacci Collection with Memoization in Java 8 Instance
Right here is our pattern Java program to implement the Fibonacci collection in Java 8. As I stated, It makes use of enhancements made in Java 8 to enhance the efficiency of the Fibonacci quantity generator, I imply computeIfAbsent() methodology added on Map class.
As I stated, this method is named Memoization and it may be used to resolve many dynamic programming-based issues. For instance, you need to use this method to calculate factorial of enormous numbers which is in any other case taking large reminiscence and time.
package deal take a look at; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Java ConcurrentHashMap computeIfAbsent Instance to resolve * Fibonacci collection with memoization. */ public class FibonacciSeriesWithMemoization{ static Map<Integer, Lengthy> cache = new ConcurrentHashMap<>(); public static void major(String[] args) { System.out.println("Calculating Fibonacci quantity with out Memoization"); fibonacci(5); System.out.println("Calculating Fibonacci with Memoization"); fibonacciWithMemoization(5); } public static lengthy fibonacci(int quantity) { if (quantity == 0) { return quantity; } if (quantity == 1) { return 1; } System.out.println("Calculating Fibonacci(" + quantity + ")"); return fibonacci(quantity - 2) + fibonacci(quantity - 1); } public static lengthy fibonacciWithMemoization(int quantity) { if (quantity == 0) { return quantity; } if (quantity == 1) { return 1; } return cache.computeIfAbsent(quantity, (n) -> { System.out.println("Not Present in Cache, Calculate Fibonacci(" + n + ")"); return fibonacciWithMemoization(quantity - 2) + fibonacciWithMemoization(quantity - 1); }); } } Output run: Calculating Fibonacci quantity with out Memoization Calculating Fibonacci(5) Calculating Fibonacci(3) Calculating Fibonacci(2) Calculating Fibonacci(4) Calculating Fibonacci(2) Calculating Fibonacci(3) Calculating Fibonacci(2) Calculating Fibonacci with Memoization Not Present in Cache, Calculate Fibonacci(5) Not Present in Cache, Calculate Fibonacci(3) Not Present in Cache, Calculate Fibonacci(2) Not Present in Cache, Calculate Fibonacci(4)
That is all about generate the Fibonacci collection in Java. You possibly can simply print collection within the console utilizing System.out.println() operate or you may retailer them in a Record. On this tutorial, you have got realized how we will use Java 8 options to implement the Fibonacci collection with memoization simply like use ConcurerntHashMap as cache and use computeIfAbsent() methodology to insert values into Map implementations like ConcurrentHashMap or just HashMap.
Thanks for studying this text thus far. In case you like this Fibonacci resolution with memorization and my rationalization then please share them with your mates and colleagues. When you have any questions or suggestions then please drop a be aware.