Monthly Archives: July 2015

Day 4 — Let’s write Null free Java code

Every Java developer whether beginner, novice, or seasoned has in his/her lifetime experienced NullPointerException. This is a true fact that no Java developer can deny. We all have wasted or spent many hours trying to fix bugs caused by NullPointerException. According to NullPointerException JavaDoc, NullPointerException is thrown when an application attempts to use null in a case where an object is required.. This means if we invoke a method or try to access a property on null reference then our code will explode and NullPointerException is thrown. You can follow the 7 Days with Java 8 series at https://shekhargulati.com/7-days-with-java-8/ Continue reading

Day 3: Let’s collect data using Stream API

On day 2, you learned that Stream API can help you work with collections in a declarative manner. We looked at the collect method, which is a terminal operation that collects the result set of a stream pipeline in a List. The collect method is a reduction operation that reduces a stream to a collection. The collect method takes a Collector which let us implement functionalities like group by, partitioning, very easily. Continue reading

Day 2 — Let’s learn about Streams

On day 1, we learnt how lambdas can help us write clean concise code by allowing us to pass behavior without the need to create a class. Lambdas is a very simple language construct that helps developer express their intent on the fly by using functional interfaces. The real power of lambdas can be experienced when an API is designed keeping lambdas in mind i.e. a fluent API that makes use of Functional interfaces(we discussed them on day 1). Continue reading

Day 1 — Let’s learn about lambdas

From today, I am kicking off 7 Days with Java 8 blog series with first blog on Lambdas. One of the most important features in Java 8 is the introduction of Lambda expressions. They make your code concise and allows you to pass behaviour around. For some time now, Java is criticised for being verbose and for lacking functional programming capabilities. With functional programming becoming more popular and relevant, Java is forced to embrace the functional style of programming. Else, Java would become irrelevant.

Java 8 is a big step forward in making the world’s most popular language adopt the functional style of programming. To support functional programming style, a language needs to support functions as first class citizen. Prior to Java 8, writing a clean functional style code was not possible without the use of an anonymous inner class boilerplate. With the introduction of Lambda expressions, functions have become first class citizen and they can be passed around just like any other variable. Continue reading