Java Online Compiler
Example: Sort String using Stream API in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Sort String using Stream API import java.util.stream.Collectors; // Required for 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 inputString = "example"; System.out.println("Original String: " + inputString); // Step 2: Convert the string to an IntStream of characters, sort, and collect String sortedString = inputString.chars() // Gets an IntStream where each int is a character's Unicode value .sorted() // Sorts the character Unicode values in ascending order .collect(StringBuilder::new, // Supplier: Creates a new StringBuilder StringBuilder::appendCodePoint, // Accumulator: Appends each sorted int (code point) StringBuilder::append) // Combiner: Used for parallel streams to merge StringBuilders .toString(); // Convert the final StringBuilder to a String // Step 3: Print the sorted string System.out.println("Sorted String: " + sortedString); } }
Output
Clear
ADVERTISEMENTS