Write a Java program using Lambda Expression to remove the vowels from a list of strings


The java program that removes vowels from a list of strings using Java Streams and regular expressions. Here's an explanation of the code:

  • An ArrayList named fruits is created to store a list of strings.
  • The fruits list is populated with various fruit names.
  • System.out.println("Given Strings : " + fruits); prints the original list of strings.
  • The program uses Java Streams to remove vowels from each string in the list:
    • fruits.stream(): This converts the fruits list into a stream of strings.
    • .map(s -> s.replaceAll("[AEIOUaeiou]", ""): This is a map operation that applies a lambda expression to each string in the stream. The lambda expression uses the replaceAll method with a regular expression [AEIOUaeiou] to remove all occurrences of vowels (both uppercase and lowercase) from each string.
    • .collect(Collectors.toList()): This collects the modified strings into a new list named withoutVowels.
  • System.out.println("Strings without Vowels : " + withoutVowels); prints the list of strings after removing the vowels.

Source Code

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
public class RemoveVowelsFromStrings
{
	public static void main(String[] args)
	{
		List<String> fruits = new ArrayList<>();
		fruits.add("Apple");
		fruits.add("Banana");
		fruits.add("Cherry");
		fruits.add("Date");
 
		System.out.println("Given Strings : " + fruits);
 
		List<String> withoutVowels = fruits.stream().map(s -> s.replaceAll("[AEIOUaeiou]", "")).collect(Collectors.toList());
 
		System.out.println("Strings without Vowels : " + withoutVowels);
	}
}

Output

Given Strings : [Apple, Banana, Cherry, Date]
Strings without Vowels : [ppl, Bnn, Chrry, Dt]

Example Programs