Monday, March 30, 2015

What is Effectively Final variable of Java 8

Apart from big three, Lambda expression, Stream API and new Date and Time API, Java 8 has also introduced a new concept called "effectively final" variable. A non final local variable or method parameter whose value is never changed after initialization is known as effectively final. Its very useful in the context of lambda expression. If you remember, prior to Java 8, we cannot use a non final local variable in anonymous class. If you have to access a local variable ( a method parameter or a variable decreed in the method itself) in Anonymous class, you have to make it final. This restriction is lessen a bit with lambda coming up. Java designers have take the need to make local variable final if its not changed once initialized. This is really a good proactive step from them, they must have anticipated frequent use of lambda expressions as compared to minimal use of Anonymous class and realized the pain to declare a local variable final every time in order to access it inside lambda. The rule has not changed, any local variable still has to be effectively final to be used inside lambda expression or anonymous inner class, its just that you don't need to use final keyword anymore, saving a few keystroke. If you don't follow this then you will get following error : "local variables referenced from a lambda expression must be final or effectively final". Let's see some examples of effectively final variables with lambdas and anonymous class and how it make life of Java developer easy.

Friday, March 27, 2015

Parsing Large JSON Files using Jackson Streaming API Example

In last couple of JSON tutorials for Java programmers, we have learned how to parse JSON using JSON-Simple library, parsing JSON array to Java array using GSon, and in this tutorial we will learn how to parse a large JSON file in Java using Jackson's Streaming API. Jackson is one of the most popular JSON processing framework and provides three main model to parse and process JSON data including Streaming API, data binding and tree model. Out of these three, Streaming works at lowest level and can be used to parse huge JSON response upto even giga bytes of size. If you are familiar with XML parsing, then you know that how difficult it is to parse huge XML files with DOM parser because it fully loads the file in memory before you can process it. In case you have low memory e.g. Android devices you can't use that to parse XML. Thankfully, XML provides SAX and StAX parsers which are streaming based and can be used to process huge files without loading them completely in memory. Out of these two, StAX is even better because it allows pull based processing where client pulls data from parser instead of parser pushing data, which is the case with SAX parser. Jackson's Streaming API is similar to StAX parser. You can pull the data you want and ignore what you don't want. Though performance doesn't come without cost, using Streaming API is little difficult then using other Jackson model which provides direct mapping between Java and Jackson objects. You have to handle all JSON data by yourself while using Streaming API.

Thursday, March 26, 2015

How to reverse array in place in Java?

Reversing an array sounds pretty easy, isn't it? It does sounds like that, because all you need to do is create an array of same size, iterate through original array from end to start and populate your new array. Boom!!, you have got an array which has elements in reverse order of original array, but problem is you have used and additional array here, which makes space complexity of your solution O(n). You cannot use this solution if array is big e.g. an array of 10 million orders and you don't have enough heap space available. Can we make it better? Can we reverse array in Java without using an additional buffer? Even If you encounter this question in your programming job interview, you will be surely asked to reverse array in place, without using an additional buffer as earlier solution takes lot of space. So now your task is to write a Java program to reverse an array in place. For the sake of this problem, you can assume that its an integer array (during interview, you should ask these question to your interviewer, because asking right question to fill the gap in requirement is a trait of good programmer and highly appreciated on both telephonic and face-to-face interviews). Key point to understand here is that you need to reverse the same array, you cannot use another array but one or two variable is fine. You are also not allowed to use any open source library or Java API which can reverse the array directly e.g. any method from java.util.Arrays class except Arrays.toString()to print arrays in Java. So now the requirement is clear, what approach comes in your mind? how do you solve this problem?