JetBrains Academy: The Collections Framework overview - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Squaring numbers:
Implement a method called pow2
which takes a collection of numbers and returns a collection of squares of these numbers.
Do not modify elements of the given collection, just create a new one and return it as the result.
class CollectionUtils {
public static Collection<Integer> pow2(Collection<Integer> numbers) {
Collection<Integer> squares = new ArrayList<>();
numbers.forEach(num -> squares.add(num*num));
return squares;
}
}
Blocked phones:
Sometimes unknown people call you and try to suggest their services. This can bother you.
To solve the problem you must implement the filterPhones
method that returns only phones that are not in the black list.
class CollectionUtils {
public static Collection<String> filterPhones(Collection<String> phones, Collection<String> blacklist) {
for (String phoneNumber : blacklist){
if (phones.contains(phoneNumber)){
phones.remove(phoneNumber);
}
}
return phones;
}
}