package chapter3.page149;
import chapter3.page143.Duck;
public class Squirrel {
private int weight;
private
String species;
public Squirrel(String theSpecies, int weight) {
if (theSpecies == null) throw new IllegalArgumentException();
species = theSpecies;
this.weight = weight;
}
public int getWeight() { return weight; }
public void setWeight(int weight) { this.weight = weight; }
public String getSpecies() { return species; }
}
package chapter3.page149;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ChainingComparator implements Comparator<Squirrel> {
public int compare(Squirrel s1, Squirrel s2) {
Comparator<Squirrel> c = Comparator.comparing(s -> s.getSpecies());
c = c.thenComparingInt(s -> s.getWeight());
return c.compare(s1, s2);
}
public static void main(String[] args) {
List<Squirrel> squirrels = new ArrayList<>();
squirrels.add(new Squirrel("Test 1", 26));
squirrels.add(new Squirrel("Test 1", 10));
squirrels.add(new Squirrel("Test 2", 45));
Collections.sort(squirrels);
}
}
The Collections.sort(squirrels) doesn't compile... Why is that?