Tuesday, November 25, 2014

Java 8 Comparator Example Using Lambda Expressions

What is the best way to learn lambda expression of Java 8? Of course by using it on your day to day programming task. Since implementing equals, hashcode, compareTo, and compare methods are some of the most common task of a Java developer, it make sense to learn how to use lambda expression to implement custom Comparator and Comparable in Java. One question comes in mind, can we use lambda expression with Comparator, because it's an old interface and may not implement functional interface annotated with @FunctionalInterface annotation? Answer to this question is Yes, you can use lambda expression to implement Comparator and Comparable interface in Java, and not just these two but to implement any interface, which just got one abstract method, remember from Java 8 interface can have non abstract methods as well e.g. default and static methods. That's why lambda expression in Java 8 is known as SAM type, where SAM stands for Single Abstract Method. This was a very important decision Java designers made, which makes it lambdas even more useful. Due to this you can use lambda expressions with Runnable, ActionListener and several other interface which got just one abstract method. By the way, you need not to worry in case of Comparator, because it is made to implement @FunctionalInterface as shown below :

Sunday, November 23, 2014

How to test if an Array contains a value in Java - Linear Search

One of the common coding question from Java interviews is how to test if an Array contains a certain value or not? This is simple question but some time interview pressure makes candidates nervous. Since array in Java doesn't have any inbuilt method for search, interviewer prefer to ask this question, to see how candidate deal with such situation. If you have good knowledge of Java API then you will immediately come to know that there are alternatives available e.g. binary search of Arrays class or taking advantage of ArrayList contains method by first converting your array to ArrayList. If you come up with those solution, Interviewer will surely ask you to write down a method to search an element in array without using any library method. You can easily solve this question if you know linear search or binary search algorithm. Linear search is very simple to implement, all you need to do is loop over array and check each value if that is the one or not. Binary search is little tricky but not too difficult either, recursive version is very natural as well. In this tutorial, though I have given two solution, one is using ArrayList, and second is using linear search, leaving binary search an exercise for you. But you must remember to sort array before using binary search. By the way to make question more challenging, I usually asked candidate to write a parametric method using generic so that it will work for any type of object array in Java.

Thursday, November 20, 2014

How to Convert a Double to Long in Java - Example Tutorial

We often need to convert a floating point number into integral number e.g. a double or float value 234.50d to long value 234L or 235L. There are couple of ways to convert a double value to long value in Java e.g. you can simply cast a double value to long or you can wrap a double value into Double object and call it's longValue() method, or using Math.round() method to round floating point value into nearest integer. Te right way to covert a double value to a long in Java really depends upon what you want to do with floating point value. If you just want to truncate the double value to remove zero and take integer value, you can simply cast double to long. If you have Double object instead of double primitive type then you can also Double.longValue() method, this doesn't do anything but just cast the double primitive wrapped inside Double object to long. It's clear from following code snippet taken from java.lang.Double class

 public long longValue() {
        return (long)value;

On the other hand if you want to round the double value to nearest long, you can use Math.round() method, this will return a long value rounded up to nearest position, for example if double value is 100.5 then it will rounded to 101, while if it is less than that e.g. 100.1 then it will be rounded to just 100. In next section we will see detailed examples of these 3 ways to convert double to long in Java.

Java67 Headline Animator