A
A
Alexzatey2020-06-08 00:53:49
Java
Alexzatey, 2020-06-08 00:53:49

You need to get words in which the first letter is a consonant and output them in one sentence separated by a space. BUT how to do it via Stream API?

It became curious to do this through streams. I am writing the following code, but I know that it is wrong. Please do not judge strictly and suggest a working solution on streams.

import java.util.stream.Stream;
import static java.lang.Character.compare;

public class jska {
    public static void main(String[] args) {

        Character[] kl = {'А', 'а', 'О', 'о', 'ы', 'Ы', 'у', 'У', 'e', 'E', 'э', 'Э'};
        String[] a = {"абра", "кадабра", "интерес"};

        Stream.iterate(0, i -> i + 1).limit(a.length)
                .filter(i -> compare(kl[i], a[i].charAt(0)) == 0)
                .forEach(System.out::println);

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
roswell, 2020-06-08
@Alexzatey

import java.util.stream.Stream;

public class Main {

    public static void main(String... args) {
        String vowels = "аеёиоуыэюя";
        String[] words = {"абра", "кадабра", "интерес", null, "Проверка", "ещё", "логика", "", "Ёж", "Сигнал"};
        Stream.of(words).filter(word -> {
            return word != null && word.length() > 0 && !vowels.contains(word.substring(0, 1).toLowerCase());
        }).forEach(word -> System.out.print(word + " "));
    }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question