Book Image

Kotlin for Enterprise Applications using Java EE

By : Raghavendra Rao K
Book Image

Kotlin for Enterprise Applications using Java EE

By: Raghavendra Rao K

Overview of this book

Kotlin was developed with a view to solving programmers’ difficulties and operational challenges. This book guides you in making Kotlin and Java EE work in unison to build enterprise-grade applications. Together, they can be used to create services of any size with just a few lines of code and let you focus on the business logic. Kotlin for Enterprise Applications using Java EE begins with a brief tour of Kotlin and helps you understand what makes it a popular and reasonable choice of programming language for application development, followed by its incorporation in the Java EE platform. We will then learn how to build applications using the Java Persistence API (JPA) and Enterprise JavaBeans (EJB), as well as develop RESTful web services and MicroServices. As we work our way through the chapters, we’ll use various performance improvement and monitoring tools for your application and see how they optimize real-world applications. At each step along the way, we will see how easy it is to develop enterprise applications in Kotlin. By the end of this book, we will have learned design patterns and how to implement them using Kotlin.
Table of Contents (13 chapters)

Lambda expressions in Kotlin

Functional programming can be advantageous when we are performing lots of different operations on data that has a known, fixed amount of variation. It provides a very elegant way to write code with immutability. It also allows you to write pure functions.

A lambda expression is an anonymous function that represents the implementation of single abstract method (SAM) of an interface. Lambda expressions deal with expressions and promote immutability, which turn functions into higher-order functions.

Let's write a lambda expression. Consider the following code for 12_LambdaExpressions_HelloWorld.kts:

val greetingLambda = { println("Hello from Lambda")}
greetingLambda()

The output is as follows:

We can invoke lambda functions directly, as in the example shown in the preceding code, or we can use invoke() as follows—greetingLambda.invoke():

val greetingLambda = { println("Hello from Lambda")}
greetingLambda.invoke()

In the next example, we will write a lambda expression that takes an argument. Consider the following code for 12a_LambdaExpressions.kts:

val greetingLambda = { user: String -> println("Hello ${user}")}
greetingLambda.invoke("Tom")

The output is as follows:

We will now write an inline lambda function to print only even numbers. Consider the following code for 12b_LambdaExpressions.kts:

listOf(0,1,2,3,4,5,6,7,8,9)
.filter{ e -> e % 2 == 0}
.forEach{ e -> println(e)}

Here, we created a list of numbers from 0-9, and then we used filter to retrieve only the even numbers. Finally, we used forEach to iterate over the numbers and print the values to the console.

The output of the preceding code is as follows: