Java Online Compiler
Example: Remove Vowels using Java 8 Streams in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Remove Vowels using Java 8 Streams import java.util.Scanner; import java.util.stream.Collectors; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Define the input string String originalString = "Stream API Example"; System.out.println("Original String: " + originalString); // Step 2: Define a string containing all vowels for quick lookup String vowels = "aeiouAEIOU"; // Step 3: Convert the string to a stream of characters, filter, and collect String stringWithoutVowels = originalString.chars() // Gets an IntStream of char values (int) .filter(c -> vowels.indexOf(c) == -1) // Filters out characters that are found in the 'vowels' string .mapToObj(c -> String.valueOf((char) c)) // Converts each int (char value) back to a String object .collect(Collectors.joining()); // Joins the filtered String objects into a single string // Step 4: Print the result System.out.println("String without Vowels: " + stringWithoutVowels); // Example with user input Scanner scanner = new Scanner(System.in); System.out.print("\nEnter another string: "); String userInput = scanner.nextLine(); String processedInput = userInput.chars() .filter(c -> vowels.indexOf(c) == -1) .mapToObj(c -> String.valueOf((char) c)) .collect(Collectors.joining()); System.out.println("Processed String: " + processedInput); scanner.close(); } }
Output
Clear
ADVERTISEMENTS