Doing null checks in Java 8 - A better way.

NullPointerExceptions are like nightmares, most of the time it happens due to a simple null check which we are missing before start using a reference variable which refer to nothing. We think we all have to deal with carefully while writing even a small piece of java code. But solving this problem is really simple. Adding a simple if condition which does a condition check.          Solving the problem (Imperative).   As it I mentioned about the best way to avoid this problem is to do a null check before using the object, especially when the object is not created by yourself. As preventive measure most of time we may need to check the reference of a variable before passing that to a method as an argument. For an instance bar is an object. Before calling a method foo,We need to check whether variable bar has valid reference or not as shown below. if(bar != null) {;   } We use this technique everywhere. But things look weird w

Sorting Objects in Java 8

Sorting is about ordering a list of object and we do this quite often. In fact, this is one of the fundamental problems we all should aware about as a developer.  When it comes to Java, Collection framework provides a well-abstracted way to sort objects. They don’t have to implement a sorting algorithm whenever they want to sort some objects. Instead, what required is an implementation of how two objects need to be compared. As it is mentioned in the title, this article is intended to describe sort operations using java8 at the high level. But before we start sort in java 8 let’s have a look at, How to perform sort using earlier versions of Java? Before Java 8 Suppose you have some User objects. And to sort the users based on the first name you will use the below code snippet. Collections.sort() accepts users- list of User objects and a Comparator. It uses an Anonymous inner class to for Comparator implementation.  As I mentioned earlier you don’t have to think m