Error code:
//Error Codes
Comparator<InspectReportListItemMpVo> comparator = new Comparator<InspectReportListItemMpVo>() {
@Override
public int compare(InspectReportListItemMpVo o1, InspectReportListItemMpVo o2) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date reportTime_o1 = simpleDateFormat.parse(o1.getReportTime());
Date reportTime_o2 = simpleDateFormat.parse(o2.getReportTime());
// return reportTime_o1.compareTo(reportTime_o2);
return reportTime_o2.compareTo(reportTime_o1);
} catch (ParseException e) {
e.printStackTrace();
return 1;
}
}
};
result.getItems().sort(comparator);
Screenshot of error reporting:
Comparison method violates its general contract
Problem-solving:
(not rigorous!) return 1 in the catch block; Change to return 0;
Supplement: rigorous writing
Collections.sort(list, new Comparator<String>() {
public int compare(String arg1, String arg2) {
/**
* Modified
*/
if(StringUtils.isBlank(arg1) ) {
if(StringUtils.isBlank(arg2)) {
return 0;
}else {
return -1;
}
}else if(StringUtils.isBlank(arg2)){
return 1;
}
return arg1.compareTo(arg2);
}
});
Cause: timsort has a reflexivity check
Timport algorithm rules:
after JDK7, after implementing the comparable interface, the following three characteristics should be met:
1) Reflexivity: the comparison results of X and y are opposite to those of Y and X
2) transitivity: X > y,y> z. Then x > z。
3) symmetry: if x = y, the comparison results of X and Z are the same as those of Y and Z.
For the above error code, if you directly return 1, assume that a is empty, a.compareto (b) = 1, and b.compareto (a) = 1, which does not satisfy reflexivity
Read More:
- Comparison between ArrayList and HashMap
- Abstract method and static method of java interface
- [Solved] error: method does not override or implement a method from a supertype
- JAVA: How to use the third-party font library method (using the third-party TTF / TTC font library method)
- Non empty judgment method: the difference between isnotempty and isnotblank. isNullOrEmpty
- Springboot Files Upload Limit Error: The field file exceeds its maximum permitted size of 1048576 bytes
- How to Solve Springboot Upload Files Error: The field XXX exceeds its maximum permitted size of 1048576 bytes
- [Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- [Solved] “status“:405,“error“ Request method ‘POST‘ not supported“
- [Solved] java.lang.IllegalAccessError: class org.springframework.data.redis.core.$ Proxy237 cannot access its superinterface org.springframework.data.redis.core.RedisConnectionUtils$RedisConnectionProxy
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- Build a mybatis and it will appear session.selectOne Method error
- [Solved] Mvel2 Error: java.lang.VerifyError method: getKnownEgressType signature
- [Solved] Error creating bean with name ‘mvcContentNegotiationManager‘: Lookup method resolution failed
- Java Web uses AJAX POST Method 405 terror [Solved]
- [Solved] IDEA java Cannot resolve method ‘getName‘ in ***
- [Solved] Invocation of init method failed; nested exception is java.lang.NoSuchMethodError:
- [Solved] The main method caused an error: Could not deploy Yarn job cluster.