Posts

Showing posts from January, 2016

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