Convert list of strings to uppercase and lowercase using Lambda expression in Java
Java Lambda Program: Exercise-3 with Solution
Write a Java program to implement a lambda expression to convert a list of strings to uppercase and lowercase.
Sample Solution:
Java Code:
// Main.java
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create a list of strings
List stringList = Arrays.asList("Red", "Green", "Blue", "PINK");
// Print the original strings
System.out.println("\nOriginal strings:");
for (String str : stringList) {
System.out.println(str);
}
// Convert strings to lowercase using lambda expression
stringList.replaceAll(str -> str.toLowerCase());
// Print the list of lowercase strings
System.out.println("\nLowercase strings:");
for (String str : stringList) {
System.out.println(str);
}
// Convert strings to uppercase using lambda expression
stringList.replaceAll(str -> str.toUpperCase());
// Print the list of uppercase strings
System.out.println("\nUppercase strings:");
for (String str : stringList) {
System.out.println(str);
}
}
}
Sample Output:
Original strings: Red Green Blue PINK Lowercase strings: red green blue pink Uppercase strings: RED GREEN BLUE PINK
Explanation:
In the above exercise, we start by creating a list of strings stringList.
The replaceAll() method applies a lambda expression that converts each string in the list to uppercase using the toUpperCase() method. This lambda expression is (str -> str.toUpperCase()). After that, the replaceAll() method applies a lambda expression that converts each string in the list to lowercase using the toLowerCase method. This lambda expression is (str -> str.toLowerCase()).
Flowchart:

Live Demo:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Java Lambda Exercises Previous: Check if string is empty.
Java Lambda Exercises Next: Filter even and odd numbers from list using 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
How do I remove repeated elements from ArrayList?
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);
Of course, this destroys the ordering of the elements in the ArrayList.
Ref: https://bit.ly/3bYIjNC
- Weekly Trends
- 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
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
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