JetBrains Academy: Filtering elements - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Bad words detecting:
You need to implement a method that returns a prepared stream for detecting bad words. The method has two parameters:
- a text in which all words are divided by single whitespaces;
- a list of bad words.
After calling collect(Collectors.toList())
the prepared stream must return the list of bad words found in the text. The result should be dictionary ordered (i.e. sorted lexicographically) and bad words shouldn't repeat.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.*;
class BadWordsDetector {
// implement method
private static Stream<String> createBadWordsDetectingStream(String text, List<String> badWords) {
return badWords.stream().filter(n -> text.contains(n)).sorted();
}
/* Do not change the code below */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] parts = scanner.nextLine().split(";");
// the first part is a text
String text = parts[0];
// the second part is a bad words dictionary
List<String> dict = parts.length > 1 ?
Arrays.asList(parts[1].split(" ")) :
Collections.singletonList("");
System.out.println(createBadWordsDetectingStream(text, dict).collect(Collectors.toList()));
}
}
Check if a number is prime:
You need to implement the isPrime
method to check whether the input number is prime or not.
It's guaranteed that input value is always greater than 1 (i.e. 2, 3, 4, 5, ....). Use the provided template for your method.
import java.util.Scanner;
import java.util.stream.*;
class PrimeNumbers {
/**
* Checking if a number is prime
*
* @param number to test >= 2
* @return true if number is prime else false
*/
private static boolean isPrime(long number) {
// write your code here
LongStream stream = LongStream.range(1, number).filter(n -> number %n == 0);
return stream.count() == 1 ? true : false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine().trim();
int n = Integer.parseInt(line);
System.out.println(isPrime(n) ? "True" : "False");
}
}
Numbers filtering:
You have two IntStream
. The first stream contains even numbers and the second stream contains odd numbers. Create the third stream that contains numbers from both streams which is divisible by 3 and 5. After calling collect(Collectors.toList())
the stream should return sorted list (ascending) of these numbers. Two first suitable numbers in the sorted list must be skipped.
public static IntStream createFilteringStream(IntStream evenStream, IntStream oddStream) {
return IntStream.concat(evenStream, oddStream)
.filter(x -> x %3 == 0)
.filter(x -> x %5 == 0)
.sorted()
.skip(2);
}
Omitting long strings:
Implement the omitLongStrings
method that must omit all string with more than or equal to 4.
Please, use streams to solve the problem.
Example: ["a", "bbb", "cccc", "dddddd"]
→ ["a", "bbb"]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.*;
public class Main {
// implement method
private static List<String> omitLongStrings(List<String> strings) {
strings = strings.stream().filter(word -> word.length() < 4).collect(Collectors.toList());
return strings;
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
List<String> list = new ArrayList<>(Arrays.asList(str.split(" ")));
omitLongStrings(list).forEach(System.out::println);
}
}