Generate all permutations of a String in Java 8 Stream

Write a Java 8 program (using Java 8 Stream API) that generates all permutations of a given string.

Java program to Generate all permutations

import java.util.stream.IntStream;
import java.util.stream.Stream;

public final class Strings {

    public static Stream<String> permuteAndReturnStream(String str) {

        if (str == null || str.isBlank()) {
            return Stream.of("");
        }

        return IntStream.range(0, str.length())
                .parallel()
                .boxed()
                .flatMap(i -> permuteAndReturnStream(str.substring(0, i) + str.substring(i + 1))
                .map(c -> str.charAt(i) + c)
                );
    }
}

Test

import java.util.stream.Stream;

public class Main {

    private static final String TEXT = "ABC";

    public static void main(String[] args) {

        System.out.println("Input text: \n" + TEXT + "\n");
        Stream<String> result = Strings.permuteAndReturnStream(TEXT);
        result.forEach((obj) -> {
        	System.out.println(obj);
        });
    }
}

Output:

Input text: 
ABC

BAC
ABC
ACB
BCA
CAB
CBA

Comments