JavaRush /Java Blog /Random EN /Coffee break #150. Let's study examples of lambda express...

Coffee break #150. Let's study examples of lambda expressions in Java. 7 Most Underrated Features of IntelliJ IDEA

Published in the Random EN group

Learning examples of lambda expressions in Java

Source: Medium In this article, we will look at lambda expressions, the basis of functional programming in Java. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 1

Lambda Expression Syntax

The syntax of lambda expressions is intuitive, easy to understand, and easy to use. A lambda expression consists of:
  1. input parameters
  2. operator arrow
  3. block of expressions/statements
The general format is:
<input-params> -> <function-code>

Examples of lambda expressions

Lambda expressions are best understood through examples, so let's look at some of them.

1. Does not accept input and does not return output

A simple lambda expression doesn't take any input and doesn't return any output—it's literally just a block of code that runs without any context.
() -> System.out.println("Here I am!");
Specifying () at the beginning, in the input parameters position, means that no parameters are passed (as with empty parentheses that follow a method that has no parameters).

2. Accepts single input, returns no output

To make a lambda expression take a parameter, we place it in the input parameters position:
name -> System.out.println("Here you are too, " + name +  "!");
NOTE : If we have one input parameter, we can omit the parentheses. We can also specify (name), this is completely acceptable.

3. Accepts multiple inputs, returns no output

When we pass multiple input parameters to a lambda expression, we must:
  • enclose parameters in parentheses
  • use comma to separate between them
(name, food) -> System.out.println("So " + name + " enjoys eating " + food + "... interesting!");

4. Contains multiple statements in a code section

Storing lambda expressions in one-line statements is considered good practice. But you can also use multiple lines:
() -> {
  System.out.println("The owl and the pussycat went to sea");
  System.out.println("in a beautiful pea green boat");
}

5. Return types are never specified

As you may have noticed, when defining a lambda expression, no return types are specified. Here's an example:
() -> System.out.println("I don't return anything!")
And one more:
() -> "I return this String!"
Both code examples are almost the same, but the second lambda returns a string. Please keep this in mind when using them. Of course, the compiler will understand the difference between them through the conditional use of function descriptors.

6. Type inference is applied automatically where possible

To determine the type, the compiler will use type inference. To do this, it looks at the execution context of the lambda expression. For the developer, this means that they won't have to worry about casting to a specific type.

7. Function Descriptor Notation

When we think about what types a lambda expression can be assigned to, it is very useful to use function descriptors. A function handle is essentially a method signature that a lambda expression (or method) provides. Its syntax is almost the same as lambda expressions, except that instead of a code section, there is an output type section. Here's an example:
<input-parameter-types> -> <output-parameter-type>

Examples of function descriptors

Here are some examples of function descriptors: () -> () A method that takes no input parameters and does not return a value. (String) -> () Method that accepts an input parameter and does not return a value. () -> (int, float) A method that takes no input parameters and returns an int and a float(int[]) -> . (SortedMap<Character, Integer>) Method that takes an int array and returns a SortedMap from Character to Integer . Having function descriptors makes it easier to evaluate type compatibility when considering what target types we can use for a lambda expression.

Conclusion

As you can see, lambda expressions are a neat and simple way to encapsulate behavior. It is one of the foundations of the functional programming paradigm introduced in Java with the release of JDK 8.

7 Most Underrated Features of IntelliJ IDEA

Source: Better Programming It's hard to believe, but many years later I still meet people who don't know about these great features in IntelliJ IDEA.

1.Find anything

It's quite difficult to remember every key combination and keep track of everything IntelliJ IDEA can do. This is why I often use search inside IntelliJ IDEA. This helps me find the menu items, tools, settings, and even files I need in one place. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 2

2. Know where you are

Before I found this feature, I used the “Select Open File” button to display the currently edited file in the project tree. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 3Now IntelliJ IDEA does this for me. This is not a default option, so you will need to set it for each new or existing project. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 4

3. SQL optimization

Did you know that the default database plugin in your IDE is more than a simple SQL executor? Given the fact that the persistence layer (I/O) is always the slowest part of the application, I always make sure that my SQL queries have a good execution plan when working with relational databases. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 5

4. Multiline editing

You may have used this feature in Sublime Text Editor before. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 6

5. Go Back / Forward

I actually often go back to where I was before by pressing the back or forward buttons. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 7Go back/forward: ⌥⌘+left/right arrow (Ctrl+Alt+left/right arrow for Win/Linux). To open recent files, press ⌘E (Ctrl+E for Win/Linux).

6. Bookmarks

I bookmark important parts of the code so I can quickly refer to them at any time. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 8To go to a bookmark, press ⌃+<number> (Ctrl+<number> for Win/Linux). I’ll say right away that I don’t use more than three bookmarks at a time, because then it becomes difficult to remember what they are for.

7. One editor for everything

I used to open other text editors like Visual Studio Code or Sublime to save parts of code, JSON, XML, and then link to them. But this is not part of the codebase (project), and IntelliJ IDEA clearly reminds you of this when you try to create or edit something like this. Then I saw one of my colleagues using Snippets and realized how genius it was. Coffee break #150.  Let's study examples of lambda expressions in Java.  7 Most Underrated Features of IntelliJ IDEA - 9New Snippet file: ⇧⌘N (Shift+Ctrl+N for Win/Linux). Snippets are available and synced across all windows of your projects. I often use them to examine and format some JSON or SQL that I got from somewhere else.

My secret recipe

The icing on the cake is a feature that saves me a ton of typing time every day: Tabnine is a JetBrains plugin for compiling AI-powered Intellij IDEA code. It's free for now, but hurry up, I don't think it will last long.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION