3 Examples to Loop Through a List in Java 8

3 ways to Loop through a List in Java
There are multiple ways to traverse or loop through a List in Java e.g. by using an Iterator , by using an enhanced for loop of Java 5, and not the forEach() method of Java 8. Given a List is an index based collection, if you know the index you can retrieve an object from List and because of this you can also use traditional for loop which keeps count for iterating List. Now the question is whether should you use the Iterator or enhanced for loop, or the forEach() method of Java 8 for looping over List in Java. Well, it depends on what you are doing with the object, if you need to remove some objects from List than iterating using Iterator is best choice to avoid ConcurrentModificationExceptionbut if you are not removing any element and just doing some operation with each element than enhanced for loop is much cleaner ways to do that. 

Java Program to Perform Binary Search in Array without Recursion

The binary search algorithm is used to search an element in the sorted array. It's faster than linear search and improves performance from O(n) to O(logN) for searching an element in the array. In order to perform the binary search, you need a sorted array, so you can either ask the user to enter array in sorted order or you should sort the array before performing the binary search. In this article, we will write a Java program which will take input from the user, both array and the number to be searched and then perform a binary search to find that number in given array. We'll not use the Collections.binarySearch() method instead we'll write our own because it's a programming exercise to test one's coding skill. In order to implement binary search, you must first know how binary search works? If you don't know the algorithm you cannot code it. So, let's first revise the binary search algorithm itself.

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.

How to use Multiple Threads in Java - Example

In order to use multiple threads in Java, you need to first define the task which will be executed by those threads. In order to create those task, you can either use Runnable or Callable interface. If you are just learning Java chose Runnable, it's simpler one, but if you are familiar with Java multithreading and want to leverage additional features offered by Callable e.g. it can throw an exception and it can also return value, then go ahead and use Callable. Once you have task ready, you need to create an instance of Thread class. You can create as many instances as you want, but beware don't create too many Thread instances in Java because both JVM and Operating system has a limit on how many threads you can create in Java. Crossing that limit will result in java.lang.OutOfmemoryError: could not create a native thread. For the purpose of this example, creating just three threads are enough.

Difference between static and nonstatic member variables in Java

In the last article, I had explained about some key difference between static and nonstatic methods in Java, and in this part, I'll explain the difference between static and nonstatic member variables in Java. The concept of static remains same, that doesn't change with method or member variables but there are still some subtle details, which every Java programmer should know and understand. As with static methods, a static member variable belongs to a class and a non-static member variable belongs to an instance. This means, the value of a static variable will be same for all instances, but the value of a non-static variable will be different for different objects. That is also referred as the state of objects. The value of nonstatic member variable actually defines the state of objects.