Day 1 (filtering melons by their type)

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");

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.245.140