Avoiding time consuming logic when using flatmapIterable

Viewed 89

I have a task to split a word into characters and then transfer each to another word.

I write some test code, use toCharArray to get char array in the flatMapIterable section, but if the target string is a bigger one, I think this method is time-consuming and sync.

My question is there is have better way or alternative to do this in reactor style and avoid time-consuming?

   final String s = "acebo";
        Mono.just(s)
                .log()
                .flatMapIterable(str -> {
                    char[] chars = str.toCharArray();
                    List<String> result = new ArrayList<>();
                    for (char aChar : chars) {
                        result.add(String.valueOf(aChar));
                    }
                    return result;
                })
                .flatMap(this::retrieveSymbolLetter)
                .subscribe(System.out::println);

    private Mono<String> retrieveSymbolLetter(Object symbol) {
        return Mono.defer(() -> {
            String result;
            switch (symbol.toString()) {
                case "a":
                    result = "aspect";
                    break;
                case "b":
                    result = "bad";
                    break;
                case "c":
                    result = "context";
                    break;
                case "d":
                    result = "dad";
                    break;
                default:
                    result = "default";
                    break;
            }
            return Mono.just(result);
        });
    }
1 Answers
Related