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;
import java.util.stream.Collectors;
public class LambdaExample {
    public static void main(String[] args) {
        List<String> status = Arrays.asList("Close", "Open", "Halt");
        String result = status
                .stream()
                .map((@NonNull var x) -> ("Market Status".concat(x)))
                .collect(Collectors.joining("\n"));
        System.out.println(result);
    }
}

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);
        System.out.println(enter);
    }
}

Output:

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);
        System.out.println(enter);
    }
}

Output:

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';
        System.out.println(str.trim().equals("Hello"));
        System.out.println(str.strip().equals("Hello"));
    }
}

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.

Reference:

https://www.journaldev.com/

1 thought on “Java 11: Lambda Expression and String functions3 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading