{
{
List<T> newElements = new ArrayList<>();
for (T nextElem: elements) {
if (selectFct.apply(nextElem))
newElements.add(nextElem);
}
return newElements;
}
}
//_test_
private void experiment() {
println("\ntest_selectElements");
List<Integer> nums = Arrays.asList(10, 11, 12, 13, 14, 15), newNums;
println("Data: " + nums);
//input integer, output boolean
Function<Integer, Boolean> isOddFct, smallNumberFct;
//fct returns true if x is odd
isOddFct = (x) -> x % 2 == 1;
newNums = FunctionPassingDrill.selectElements(nums, isOddFct);
println("Odd Numbers: " + newNums);
//fct returns true if x is equal or less than 12
smallNumberFct = (x) -> x <= 12;
newNums = FunctionPassingDrill.selectElements(nums, smallNumberFct);
println("Smaller Numbers (less than or equal to 12): " + newNums);
List<String>
names = Arrays.asList("Asha", "Acheampong", "Mo"),
newNames;
println("Names: " + names);
//input string, output boolean
Function<String, Boolean> isShortStringFct;
isShortStringFct = (str) -> str.length() < 5;
newNames = FunctionPassingDrill.selectElements(names, isShortStringFct);
println("Shorter Strings: " + newNames);
}