Java 8 – Remove Vowels from a String

Introduction

Removing vowels from a string is a common task in text processing, often used in scenarios like text filtering, data cleaning, or creating word puzzles. With Java 8, this task can be accomplished efficiently using the Stream API. In this guide, we’ll walk you through how to create a Java program that removes all vowels from a given string using Java 8 streams.

Problem Statement

The task is to create a Java program that:

  • Accepts a string as input.
  • Uses Java 8 Streams to filter out vowels from the string.
  • Outputs the string with all vowels removed.

Example 1:

  • Input: "Hello World"
  • Output: "Hll Wrld"

Example 2:

  • Input: "Java Programming"
  • Output: "Jv Prgrmmng"

Solution Steps

  1. Input String: Start with a string that can either be hardcoded or provided by the user.
  2. Define Vowels: Create a set of characters representing the vowels (both lowercase and uppercase).
  3. Filter Out Vowels: Convert the string into a stream of characters and filter out the vowels.
  4. Collect and Display the Result: Collect the filtered characters back into a string and print the result.

Java Program

Java 8 Program to Remove Vowels from a String

import java.util.Set;
import java.util.stream.Collectors;

/**
 * Java 8 Program to Remove Vowels from a String
 * Author: https://www.rameshfadatare.com/
 */
public class RemoveVowels {

    public static void main(String[] args) {
        // Step 1: Take input string
        String input = "Hello World";

        // Step 2: Remove vowels using streams
        String result = removeVowels(input);

        // Step 3: Display the result
        System.out.println("String without vowels: " + result);
    }

    // Method to remove vowels from a string
    public static String removeVowels(String input) {
        // Define vowels
        Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');

        // Filter out vowels and collect the result
        return input.chars()
                .mapToObj(c -> (char) c)
                .filter(c -> !vowels.contains(c))
                .map(String::valueOf)
                .collect(Collectors.joining());
    }
}

Explanation of the Program

  • Input Handling: The program uses the string "Hello World" as an example input. This can be modified to accept input from the user if required.

  • Defining Vowels: A Set of characters is used to store both lowercase and uppercase vowels (a, e, i, o, u, A, E, I, O, U).

  • Filtering Vowels: The chars() method converts the input string into an IntStream of character codes. These are then converted into a stream of Character objects using mapToObj. The filter() method removes any characters that are in the vowels set.

  • Collecting the Result: The filtered characters are converted back into a string using Collectors.joining(), which concatenates the characters without any separators.

  • Output: The program prints the resulting string after all vowels have been removed.

Output Example

Example 1:

Input: Hello World
Output: String without vowels: Hll Wrld

Example 2:

Input: Java Programming
Output: String without vowels: Jv Prgrmmng

Advanced Considerations

  1. Case Sensitivity: The program is designed to remove both lowercase and uppercase vowels, ensuring that the result is case-insensitive.

  2. Handling Special Characters: The program does not remove spaces, punctuation, or other special characters. It only filters out vowels, making it suitable for general text processing tasks.

  3. Performance Considerations: This approach is efficient for typical string lengths and leverages the functional programming features of Java 8. It provides a clear and concise method for removing vowels from a string.

Conclusion

This Java 8 program efficiently removes vowels from a string using streams. By taking advantage of the Stream API, you can create a concise and readable solution to this common text processing task. Whether you’re cleaning data, creating puzzles, or just experimenting with string manipulation, this approach offers a modern and effective way to handle vowel removal in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top