Concatenate strings using Lambda expression in Java
Java Lambda Program: Exercise-10 with Solution
Write a Java program to implement a lambda expression to concatenate two strings.
Sample Solution:
Java Code:
import java.util.function.BiFunction;
public class Main {
public static void main(String[] args) {
// Define the concatenate lambda expression
BiFunction<String, String, String> concatenate = (str1, str2) -> str1 + str2;
// Concatenate two strings using the lambda expression
String string1 = "Good ";
String string2 = "Morning!";
System.out.println("Original strings: " + string1 + ", " +string2);
String result = concatenate.apply(string1, string2);
// Print the concatenated string
System.out.println("\nConcatenated string: " + result);
}
}
Sample Output:
Original strings: Good , Morning! Concatenated string: Good Morning!
Explanation:
In the above exercise -
From the java.util.function package, we import the BiFunction functional interface.
In the main() method, we define a lambda expression using the BiFunction<String, String, String>. This functional interface represents a function that accepts two String arguments and produces a String result.
The lambda expression "concatenate" takes two strings str1 and str2 as input and concatenates them using the + operator.
After defining the lambda expression, we use it to concatenate two strings by calling the apply method on the lambda expression and passing the two strings as arguments. The result is stored in the result variable, which is a String.
Flowchart:

Live Demo:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Java Lambda Exercises Previous: Check prime number using Lambda expression in Java.
Java Lambda Exercises Next: Find maximum and minimum values with Lambda expression in Java.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
Java Classes
A Java class is declared with the keywords public class along with a unique class name mirroring its file name. For example, in a file Hello.java in project helloworld:
package helloworld; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * @author seth * A GUI hello world. */ public class Hello { // this is an empty class }
You can declare variables and functions inside a class. In Java, variables within a class are called fields.
Ref: https://red.ht/3sL3sSl
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook