Java 8 streams API

My experience with querying a collection has always been clumsy. Writing iterative loops, adding nested if conditions and above all the hack of maintaining intermediary collection to store results. On top of that the brutal indicators from SonarQube – cyclomatic complexity. During all this, I felt there is a common pattern for filtering, sorting and there has to be an expressive way to achieve this functionality(someone said more like SQL!!!). With my love towards multiple technologies (Ok I accept I am a polyglot coder), I knew .Net already had an answer to this – LINQ. Simply have a collection and write a query to extract data. Something like below
Continue reading

What is lambda? and why do we need it?

When I started coding GUI application using Java, I realized my code was cluttered with too many anonymous classes. Each time, I had to handle an event the inlined implementation of ActionListener introduced nested and unreadable lines of code. How many of you have written code like below

signUpUserBtn.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){

Continue reading

Java 8 – Method Reference – Is it Magic?

Amongst the notable features in Java8, Method Reference adds Shorthand syntax for a lambda expression. In most cases, Lambda expression does nothing more than just calling a single method. If you find yourself doing that most often, its time to look at Method reference. It can be used to replace entire Lambda expression, let’s have a look at following example
Continue reading

Java 8 – What is functional Interface?

Functional interface is a new addition to Java 8. In simple terms, an interface which has one single abstract method is qualified as Functional interface. The built-in classes in JDK are full of examples of Functional Interfaces(Runnable, Callable, ActionListener). Java 8 formalized such interface definition with @FunctionalInterface annotation. FunctionalInterface is also useful while creating lambda expressions. Those who are not familiar with Lambda expression, it was again a new feature introduced in Java 8. Lambda helps developers to write concise code. Its expressive syntax saves developer’s time. Before Lambda, for every single method declaration of an interface, developers had to create Class. Eventually, an application source code is filled with many small classes which just provide an implementation for one single method. FunctionalInterface and Lambda expressions are considered to be best practice for writing neat code. Lets have a look at how JavaDoc describes FunctionalInterface
Continue reading