One day, Mark asked us to provide a feature for filtering melons by their type. As a result, we created a utility class named Filters and implemented a static method that takes a list of melons and the type to filter on as arguments.
The resulting method is pretty straightforward:
public static List<Melon> filterByType(
List<Melon> melons, String type) {
List<Melon> result = new ArrayList<>();
for (Melon melon: melons) {
if (melon != null && type.equalsIgnoreCase(melon.getType())) {
result.add(melon);
}
}
return result;
}
Done! Now, we can easily filter melons by type, as shown in the following example:
List<Melon> bailans = Filters.filterByType(melons, "Bailan");