JetBrains Academy: Functional interfaces and lambda expressions - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Max of two integers:
Write a lambda expression that accepts two integers arguments and returns max of them.
Try not to use the Math library.
Solution format: Submit your lambda expression in any valid format with ; on the end.
(x, y) -> x > y ? x : y;
Next even number:
Write a lambda expression that accepts a long value and returns a next even number.
It is guaranteed an input number is non-negative.
Solution format: Submit your lambda expression in any valid format with ; on the end.
val -> val %2 == 0 ? val+2 : val+1;
Production of all numbers in the range:
Write a lambda expression that accepts two long arguments as a range borders and calculates (returns) product of all numbers in this range (inclusively). It's guaranteed that 0 <= left border <= right border.
If left border == right border then the result is any border.
Solution format: Submit your lambda expression in any valid format with ; on the end.
(leftBorder, rightBorder) -> {
long result = 1;
for (int i = 0; i <= rightBorder-leftBorder; i++){
result = result * (leftBorder+i);
}
return result;
};
Getting distinct strings:
Write a lambda expression that accepts a list of strings and returns new list of distinct strings (without repeating). The order of elements in the result list may be any (elements will be sorted by the testing system).
If the input list doesn't contain any strings then the result list should be empty.
Hints: it is possible to use sets, maps, lists and so on for helping.
Solution format: Submit your lambda expression in any valid format with ; on the end.
listOfStrings -> {
List<String> newListOfDistinctStrings = new ArrayList<>();
for (String word : listOfStrings){
if (!newListOfDistinctStrings.contains(word)){
newListOfDistinctStrings.add(word);
}
}
return newListOfDistinctStrings;
};
or:
listOfStrings -> {
Set<String> distinctSet = new HashSet<>(listOfStrings);
return new ArrayList<>(distinctSet);
};
or:
listOfStrings -> new ArrayList<>(new HashSet<>(listOfStrings));
Closures - 1:
Using closure write a lambda expression that calculates ax2 + bx + c where a, b, c are context final variables. They will be available in the context during testing. Note, the result is double.
Solution format: Submit your lambda expression in any valid format with ; on the end.
x -> a * Math.pow(x, 2) + b * x + c;
Closures - 2:
Using closure write a lambda expression that adds prefix (before) and suffix (after) to its single string argument; prefix and suffix are final variables and will be available in the context during testing.
All whitespaces on the both ends of the argument must be removed. Do not trim prefix, suffix and the result string.
Solution format: Submit your lambda expression in any valid format with ; on the end.
argument -> prefix + argument.trim() + suffix;
A lambda expression with seven arguments:
Write a lambda expression that accepts seven (!) string arguments and returns a string in uppercase concatenated from all of them (in the order of arguments).
Solution format: Submit your lambda expression in any valid format with ; on the end.
// with Locale defined:
(a, b, c, d, e, f, g) -> (a+b+c+d+e+f+g).toUpperCase(java.util.Locale.UK);
//without Locale defined:
(a, b, c, d, e, f, g) -> (a+b+c+d+e+f+g).toUpperCase();
Ternary predicate:
You need to write your own functional interface (TernaryIntPredicate) and use it with a lambda expression. The interface must have a single non-static (and non-default) method test
with three int arguments that returns boolean value.
Besides, you need to write a lambda expression with three int arguments using your TernaryIntPredicate
.
The lambda expression has to return true if all passed values are different otherwise false. The name of the instance is allValuesAreDifferentPredicate
. It should be a static field.
Important. Use the provided template for your solution. Do not change it!
@FunctionalInterface
public interface TernaryIntPredicate {
Boolean test(Integer num1, Integer num2, Integer num3);
}
public static final TernaryIntPredicate allValuesAreDifferentPredicate = (a, b, c) -> !a.equals(b) && !b.equals(c) && !a.equals(c);