Question: Given the following Boxer class: (a) provide the code for a Comparator called BoxerComparator that will allow us to sort a list of boxers in
Given the following Boxer class:
(a) provide the code for a Comparator called BoxerComparator that will allow us to sort a list of boxers in ascending order, by weight. (b) Also provide a predicate as a lambda expression that will take the sorted list produced by your comparator, and will return a sublist of the heavyweight boxers (as indicated by their enum value HEAVY).
Your solution should work with the code below. Fill in the code for your lambda expression in the commented section of the filter() method. A stub the BoxerComparator is included here; you may use that as your starting point for your Comparator.
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class Boxer { private String name; private double weight; private WEIGHT_CLASS weightClass; public enum WEIGHT_CLASS { FEATHER, MIDDLE, HEAVY }; public Boxer(String name, double weight) { this.name = name; this.weight = weight; if (weight < 135.0) { this.weightClass = WEIGHT_CLASS.FEATHER; } else if (weight < 180.0 ) { this.weightClass = WEIGHT_CLASS.MIDDLE; } else { this.weightClass = WEIGHT_CLASS.HEAVY; } } public String getName() { return this.name; } public double getWeight() { return this.weight; } public WEIGHT_CLASS getWeightClass() { return this.weightClass;} @Override public String toString() { return getName(); } // Smoke test to verify our boxers public static void main(String [] args) { List
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
