13.3 Lambda Expressions and Anonymous Classes
As we have seen in this chapter so far, both anonymous classes and lambda expressions can be used to provide implementation of functional interfaces. Example 13.3 illustrates using both anonymous classes and lambda expressions for this purpose.
A common operation on elements in a collection is to select those elements that satisfy certain criteria. This operation is called filtering. Given a list of words, we would like to filter this list for one-word palindromes—that is, words spelled the same forward and backward. For example, “anana” is a palindrome, but “banana” is not.
Example 13.3 defines the generic method filterList() at (7) for filtering a list. Its first parameter is a list of type T to filter, and the filtering criteria is given by the second parameter which is a generic functional interface. This functional interface, Predicate<T>, specifies the boolean abstract method test(T t). The argument passed to the method must implement the Predicate<T> interface, supplying the implementation of the boolean method test() that actually determines if an element satisfies the criteria. The test() method is an example of a predicate—that is, a function that takes an argument and returns a boolean value.
The following boolean expressions are used to determine whether a word (given by the reference str) is a case-sensitive or case-insensitive palindrome, respectively:
str.equals(new StringBuilder(str).reverse().toString())
str.equalsIgnoreCase(new StringBuilder(str).reverse().toString())
Whether a string is a palindrome is determined by comparing the string for equality with the result of reversing the string using a StringBuilder.
Example 13.3 Filtering an ArrayList<E>
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class FunWithPalindromes {
public static void main(String[] args) {
List<String> words = new ArrayList<>();
words.add(“Otto”); words.add(“ADA”); words.add(“Alyla”);
words.add(“Bob”); words.add(“HannaH”); words.add(“Java”);
System.out.println(“List of words: ” + words);
System.out.println(“———–Using Anonymous Classes———————“);
// Use an anonymous class to filter for palindromes (case sensitive). (1)
List<String> palindromesA = filterList(words,
new Predicate<String>() {
@Override public boolean test(String str) {
return str.equals(new StringBuilder(str).reverse().toString());
}
}
);
System.out.println(“Case-sensitive palindromes: ” + palindromesA);
// Use an anonymous class to filter for palindromes (case insensitive). (2)
List<String> palindromesB = filterList(words,
new Predicate<String>() {
@Override public boolean test(String str) {
return str.equalsIgnoreCase(
new StringBuilder(str).reverse().toString());
}
}
);
System.out.println(“Case-insensitive palindromes: ” + palindromesB);
System.out.println(“———–Using Lambda Expressions——————–“);
Predicate<String> predicate1 = str ->
str.equals(new StringBuilder(str).reverse().toString()); // (3)
List<String> palindromes1 = filterList(words, predicate1); // (4)
System.out.println(“Case-sensitive palindromes: ” + palindromes1);
Predicate<String> predicate2 = str ->
str.equalsIgnoreCase(new StringBuilder(str).reverse().toString());// (5)
List<String> palindromes2 = filterList(words, predicate2); // (6)
System.out.println(“Case-insensitive palindromes: ” + palindromes2);
}
/**
* Filters a list according to the criteria of the predicate.
* @param list List to filter
* @param predicate Provides the criteria for filtering the list
* @return List of elements that match the criteria
*/
public static <E> List<E> filterList(List<E> list, // (7)
Predicate<E> predicate) {
List<E> result = new ArrayList<>();
for (E element : list) {
if (predicate.test(element)) { // (8)
result.add(element);
}
}
return result;
}
}
Output from the program:
List of words: [Otto, ADA, Alyla, Bob, HannaH, Java]
———–Using Anonymous Classes———————
Case-sensitive palindromes: [ADA, HannaH]
Case-insensitive palindromes: [Otto, ADA, Alyla, Bob, HannaH]
———–Using Lambda Expressions——————–
Case-sensitive palindromes: [ADA, HannaH]
Case-insensitive palindromes: [Otto, ADA, Alyla, Bob, HannaH]