keySet() vs entrySet vs values() Example in Java Map

The java.util.Map interface provides three methods keySet(), entrySet() and values() to retrieve all keys, entries (a key-value pair), and values. Since these methods directly come from the Map interface, you can use it with any of the Map implementation class e.g. HashMap, TreeMap, LinkedHashMap, Hashtable, ConcurrentHashMap, and even with specialized Map implementations like EnumMap, WeakHashMapand IdentityHashMap. In order to become a good Java developer, it's important to understand and remember key classes Java API e.g. Java's Collection framework. In this article, we will not only learn the difference between keySet(), entrySet() and values() methods, but also learn how to use them in Java program by looking at a simple example.

Java Program to calculate Area of Circle

You can calculate the area of a circle in Java by just writing a class and a method. All you need to know is the formula to calculate the area of circle and trick to get input from the user in Java. If you know these two already than the calculating area of a circle is very easy. Since every program must have a class in Java, we need to create a class. I have created a class called Circle for our examples purpose. Now, since the execution of Java program starts from the main method, I have provided a public static void main() method in our program. This single method is enough to put all the code required for this program e.g. getting input from the user, calculating area and displaying area of a circle in the console. But, for better coding experience purpose, we'll just create a method to calculate the area of a circle.

Fibonacci Series in Java Using Recursion

Fibonacci series in Java
Write a Java program to print Fibonacci series up to a given number or create simple Java program to calculate Fibonacci number is common Java questions on fresher interview and homework. Fibonacci series is also a popular topic on various programming exercises in school and colleges. Fibonacci series is series of natural number where next number is equivalent to the sum of previous two number e.g. fn = fn-1 + fn-2. The first two numbers of Fibonacci series is always 1, 1. In this Java program example for Fibonacci series, we create a function to calculate Fibonacci number and then print those numbers on Java console. Another twist in this questions is that sometime interviewer asks to write a Java program for Fibonacci numbers using recursion, so it's better you prepare for both iterative and recursive version of Fibonacci number.