Reputation: 31
public static final Comparator<Summary> COMPARATOR = (courseModel1, courseModel2) -> {
if (courseModel1.department.compareTo(courseModel2.department) > 0) {
return courseModel1;
}
if (courseModel1.department.compareTo(courseModel2.department) < 0) {
return courseModel2;
}
if (courseModel1.department.compareTo(courseModel2.department) == 0) {
if (courseModel1.number.compareTo(courseModel2.number) > 0) {
return courseModel1;
}
if (courseModel1.number.compareTo(courseModel2.number) < 0) {
return courseModel2;
}
if (courseModel1.number.compareTo(courseModel2.number) == 0) {
if (courseModel1.title.compareTo(courseModel2.title) > 0) {
return courseModel1;
}
if (courseModel1.title.compareTo(courseModel2.title) < 0) {
return courseModel2;
}
}
}
// (ERROR: missing a return statement here)
};
Upvotes: 0
Views: 224
Reputation: 2454
As mentioned in the documentation for the method compare
Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
So your code needs to return an integer instead of the objects.
If the first argument is less than the second, return -1
,
If the first argument is greater than the second, return 1
,
If the objects are equal return 0
.
Currently, it's returning one of the parameters.
The cause of missing a return statement
compilation error is a missing branch, your code can either use if/else
to ensure all branches are covered, instead of just if
s, or add a default return value at the end.
Edit, more detailed info below:
What happens if the
if
pointed by the blue arrows is false? There's no code to indicate it.
Upvotes: 2
Reputation: 40062
First, Comparators return an int
describing the relationship between two objects. They do not return the objects under comparison.
So you may want to simplify your comparator as follows. This works by first checking for equality(== 0). If not equal, return either -1 or 1 depending on the subsequent comparison (it must be one or the other). Otherwise, continue to the next test and check for equality of numbers and if equal, just return the numeric result of comparing titles, regardless. It would look like this.
Comparator<Summary> COMPARATOR =
(courseModel1, courseModel2) -> {
if (courseModel1.department.compareTo(
courseModel2.department) != 0) {
return courseModel1.department
.compareTo(courseModel2.department);
}
if (courseModel1.number
.compareTo(courseModel2.number) != 0) {
return courseModel1.number
.compareTo(courseModel2.number);
}
// in the last case, just return the overall comparison (-1,0, or 1).
return courseModel1.title
.compareTo(courseModel2.title);
};
But this could be simplified further by using the methods provided by the Comparator
interface. In each case it will return the result of comparing Summary fields based which is less or greater. If they are equal, the comparator will then try the number, followed by the title.
Comparator<Summary> comp =
Comparator.comparing(Summary::getDepartment)
.thenComparing(Summary::getNumber)
.thenComparing(Summary::getTitle);
Regardless of the form of the Comparator, you would use it like this.
Summary sum1 = ...
Summary sum2 = ...
int cmp = COMPARATOR.compare(sum1,sum2);
if (cmp < 0) {
// do something with sum1.
} else if (cmp > 0) {
// do something with sum2.
} else {
// take some action as required.
}
The class being compared
class Summary {
public String department;
public String title;
public Integer number;
public String getDepartment() {
return department;
}
public String getTitle() {
return title;
}
public Integer getNumber() {
return number;
}
}
Upvotes: 0