Java 11: Lambda Expression and String functions

Reading Time: 3 minutes

Java 11 comes up with a lot of new features, thus easing out the life of a programmer. In this blog, I will discuss some of them.

First and foremost great news about java 11, we need not to compile the java source file using command javac. A java program can now easily run using java command as the compilation happens implicitly.

Lambda Parameter Syntax:

Java 11 allows the declare the formal parameters . Using the var, this can be done for the implicitly typed lambda parameters. For example:

import lombok.NonNull;
import java.util.Arrays;
import java.util.List;
public class LambdaExample {
    public static void main(String[] args) {
        List<String> status = Arrays.asList("Close", "Open", "Halt");
        String result = status
                .map((@NonNull var x) -> ("Market Status".concat(x)))

The advantage of using var is that when we need to apply annotations to lambda parameters, the type needs to be specified. So, using var provides uniformity without losing brevity.

Some of the shortcomings of var in lambda parameters are:

  • Parenthesis needs to be applied while using var, which otherwise could have been skipped with single parameter lambda.
  • In case there are multiple parameters, if var is applied to one of them, we need to apply to all of them.
  • If we are using var to represent the type of one of the parameters, we need to use var for the rest of them. Meaning, we cannot use a type like String with one and var with another.

String methods:

There are a lot of new string methods which have been introduced in java 11. Some of the commonly used methods are:

1. repeat(int) :

This helps to simply repeat a string. The number of times you want to repeat it can be specified in the parameter. For example:

public class RepeatExample {
    public static void main(String[] args) {
        String enter = "Welcome ! ".repeat(3);


What happens if the count is negative??

Let’s see with the help of an example:

public class RepeatExample {
    public static void main(String[] args) {
        String enter = "Welcome ! ".repeat(-1);


So, in such a case, it throws an IllegalArgumentException.

2. strip()

This method is similar to our old trim method, but the reason of adding this is that strip() is Unicode-aware. To further elaborate, strip() uses Character.isWhitespace() which uses Unicode standard to identify then whitespace characters. Here is an example to differentiate between these two methods:

public class TrimVsStrip {
    public static void main(String[] args) {
        String str = '\u2001' + "Hello" + '\u2001';

The output of the trim() statement is false but for strip(), it is true. This indicates that the strip() methods removes Unicode whitespace but trim() does not.

Similarly, we have stripLeading(), stripTrailing() methods as well for stripping the trailing and leading spaces.


Written by 

Jasmine is a Software Consultant, with an experience of more than 11 months. She is familiar with Object Oriented Programming Paradigms and has a good command over C/C++. Apart from being a programmer, She is also fond of relational database technologies such as PostGreSQL & MySQL. Apart from that, work life balance is one of the most important thing for her. Her hobbies include watching netflix,chatting ,surfing net and writing.

Leave a Reply