- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
<分区>
我有这门课:
class Column implements Comparable<Column> {
private final float startX;
private final float endX;
public Column(float startX, float endX) {
this.startX = startX;
this.endX = endX;
}
public boolean isSameColumn(Column c) {
return c.startX <= this.startX && this.startX < c.endX || this.startX <= c.startX && c.startX < this.endX;
}
@Override
public int hashCode() {
return Objects.hash(startX, endX);
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
final Column other = (Column) obj;
return this.startX == other.startX && this.endX == other.endX;
}
@Override
public int compareTo(Column o) {
return isSameColumn(o) ? 0 : Float.compare(this.startX, o.startX);
}
}
在我看来,compareTo
方法符合 Comparator 协定,即使它与 equals
(故意)不一致 - 这应该不是问题根据 the javadoc :
It is generally the case, but not strictly required that
(compare(x, y)==0) == (x.equals(y))
但是,我有时会得到:
java.lang.IllegalArgumentException: Comparison method violates its general contract!
例如,下面的代码抛出一个异常(也在ideone上——它有点长,但大部分是测试数据)。
另请注意,在流中的 distinct()
之前运行 sorted()
可解决问题。
public static void main (String[] args) {
String points = "54.199997, 88.399216, 135.2, 250.09616, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 168.19669, 178.7, 207.49712, " +
"135.2, 168.19669, 370.1, 391.69785, 108.2, 120.79874, 135.2, 189.49884, 135.2, 180.7966, 370.1, 391.69785, 108.2, 115.39928, 135.2, 215.59856, " +
"135.2, 172.69742, 377.9, 391.6986, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 370.1, 391.69785, " +
"108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, 391.69824, " +
"108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 189.49884, " +
"135.2, 180.7966, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, 391.69824, " +
"108.2, 120.79874, 135.2, 217.09616, 135.2, 170.59763, 377.9, 391.6986, 108.2, 120.79874, 135.2, 208.99689, 135.2, 163.39717, 374.0, 391.69824, " +
"108.2, 120.79874, 135.2, 214.6982, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 189.49884, 135.2, 180.7966, 374.0, 391.69824, " +
"135.2, 250.09616, 517.1, 544.6972, 108.2, 120.79874, 135.2, 198.79779, 135.2, 187.69778, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, " +
"135.2, 172.69742, 377.9, 391.6986, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 370.1, 391.69785, " +
"108.2, 120.79874, 135.2, 217.39865, 135.2, 175.69711, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 219.49155, " +
"135.2, 163.39717, 374.0, 391.69824, 108.2, 120.79874, 135.2, 201.19598, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 218.59897, " +
"135.2, 189.49573, 370.1, 391.69785, 108.2, 120.79874, 135.2, 218.59897, 135.2, 189.49573, 370.1, 391.69785, 108.2, 120.79874, 135.2, 209.5979, " +
"135.2, 163.39717, 370.1, 391.69785, 108.2, 120.79874, 135.2, 174.19609, 135.2, 172.69742, 370.1, 391.69785, 108.2, 120.79874, 135.2, 174.19609, " +
"135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 189.49884, 135.2, 180.7966, 374.0, 391.69824, 108.2, 120.79874, 135.2, 214.39862, " +
"135.2, 178.9956, 374.0, 391.69824, 108.2, 120.79874, 135.2, 161.59735, 135.2, 196.39772, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, " +
"108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, " +
"108.2, 120.79874, 135.2, 195.1961, 135.2, 208.6958, 135.2, 178.0957, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 118.99892, " +
"135.2, 144.1991, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 118.99892, 135.2, 193.09724, 374.0, 391.69824, 108.2, 120.79874, " +
"135.2, 185.59799, 135.2, 189.19801, 377.9, 391.6986, 108.2, 120.79874, 135.2, 202.997, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, " +
"135.2, 217.69742, 135.2, 163.39717, 374.0, 391.69824, 135.2, 250.09616, 517.1, 544.6972, 108.2, 120.79874, 135.2, 187.69786, 135.2, 172.69742, " +
"370.1, 391.69785, 108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, 120.79874, 135.2, 208.99646, 135.2, 182.8964, " +
"374.0, 391.69824, 108.2, 120.79874, 135.2, 214.6982, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 178.69823, 135.2, 163.39717, " +
"374.0, 391.69824, 108.2, 120.79874, 135.2, 178.69844, 135.2, 172.69742, 370.1, 391.69785, 108.2, 120.79874, 135.2, 178.69844, 135.2, 172.69742, " +
"377.9, 391.6986, 108.2, 120.79874, 135.2, 221.29475, 135.2, 163.39717, 374.0, 391.69824, 108.2, 120.79874, 135.2, 221.29475, 135.2, 163.39717, " +
"370.1, 391.69785, 524.7, 553.7995, 54.199997, 88.399216, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 440.6, 468.19724, 108.2, 125.59826, " +
"135.2, 179.2979, 187.7, 210.49771, 135.2, 208.69688, 370.1, 391.69785, 108.2, 120.79874, 135.2, 178.69844, 135.2, 172.69742, 374.0, 391.69824, " +
"517.1, 544.6972, 54.199997, 88.399216, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 436.7, 468.19687, 108.2, 118.698944, 135.2, " +
"196.99606, 135.2, 172.99727, 360.2, 391.69687, 108.2, 120.79874, 135.2, 201.19598, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, " +
"209.5979, 135.2, 163.39717, 370.1, 391.69785, 108.2, 120.79874, 135.2, 189.49884, 135.2, 180.7966, 374.0, 391.69824, 108.2, 120.79874, 135.2, " +
"208.69637, 135.2, 175.39758, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, 115.39928, 135.2, " +
"215.59856, 135.2, 172.69742, 377.9, 391.6986, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 436.7, " +
"468.19687, 108.2, 118.698944, 135.2, 208.09802, 135.2, 162.79724, 364.1, 391.69724, 135.2, 250.09616, 517.1, 544.6972, 108.2, 120.79874, 135.2, " +
"178.0957, 135.2, 163.39717, 374.0, 391.69824, 108.2, 120.79874, 135.2, 185.59691, 135.2, 163.39717, 370.1, 391.69785, 108.2, 115.39928, 135.2, " +
"178.69844, 135.2, 172.69742, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 125.59826, 135.2, 179.2979, 187.7, 210.49771, 135.2, " +
"213.79517, 370.1, 391.69785, 517.1, 544.6972, 54.199997, 88.399216, 108.2, 120.79874, 135.2, 214.6982, 135.2, 172.69742, 370.1, 391.69785, 108.2, " +
"120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 180.79544, 135.2, 167.29678, 374.0, 391.69824, 108.2, " +
"120.79874, 135.2, 165.49696, 135.2, 167.29678, 374.0, 391.69824, 108.2, 120.79874, 135.2, 165.49696, 135.2, 167.29678, 374.0, 391.69824, 108.2, " +
"115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, 135.2, 172.69742, 377.9, 391.6986, 108.2, " +
"115.39928, 135.2, 190.69446, 135.2, 190.69894, 377.9, 391.6986, 108.2, 115.39928, 135.2, 165.49861, 135.2, 195.19905, 377.9, 391.6986, 517.1, " +
"544.6972, 54.199997, 88.399216, 108.2, 118.99892, 135.2, 205.99895, 135.2, 195.79625, 374.0, 391.69824, 517.1, 544.6972, 54.199997, 88.399216, " +
"108.2, 118.99892, 135.2, 201.49861, 374.0, 391.69824, 108.2, 118.698944, 135.2, 201.79861, 135.2, 183.19757, 364.1, 391.69724, 517.1, 544.6972, " +
"54.199997, 87.499214, 108.2, 125.59826, 135.2, 182.2976, 190.7, 214.39763, 135.2, 215.897, 370.1, 391.69785, 517.1, 544.6972, 54.199997, " +
"87.499214, 108.2, 118.99892, 135.2, 211.99535, 370.1, 391.69785, 108.2, 121.69865, 135.2, 191.89688, 135.2, 214.39604, 364.1, 391.69724, 108.2, " +
"121.69865, 135.2, 191.89688, 135.2, 214.39604, 370.1, 391.69785, 108.2, 125.59826, 135.2, 222.19763, 135.2, 215.2975, 370.1, 391.69785, 108.2, " +
"115.39928, 135.2, 199.99884, 135.2, 193.99792, 377.9, 391.6986, 108.2, 115.39928, 135.2, 175.69751, 135.2, 166.39688, 374.0, 391.69824, 135.2, " +
"250.09616, 523.1, 544.6978, 108.2, 115.39928, 135.2, 198.797, 135.2, 171.1964, 374.0, 391.69824, 108.2, 115.39928, 135.2, 198.797, 135.2, " +
"171.1964, 377.9, 391.6986, 108.2, 115.39928, 135.2, 198.797, 135.2, 171.1964, 377.9, 391.6986, 523.1, 544.6978, 54.199997, 87.499214, 108.2, " +
"118.698944, 135.2, 174.19894, 135.2, 161.59735, 370.1, 391.69785, 108.2, 120.79874, 135.2, 178.69844, 135.2, 180.7966, 377.9, 391.6986, 108.2, " +
"120.79874, 135.2, 189.49884, 135.2, 180.7966, 370.1, 391.69785, 108.2, 115.39928, 135.2, 160.99742, 135.2, 195.79776, 377.9, 391.6986, 523.1, " +
"544.6978, 54.199997, 87.499214, 108.2, 120.79874, 135.2, 217.69742, 135.2, 163.39717, 370.1, 391.69785, 108.2, 120.79874, 135.2, 212.59627, " +
"135.2, 172.69742, 374.0, 391.69824, 108.2, 120.79874, 135.2, 187.09769, 135.2, 163.39717, 377.9, 391.6986, 108.2, 115.39928, 135.2, 215.59856, " +
"135.2, 172.69742, 377.9, 391.6986, 108.2, 120.79874, 135.2, 195.1961, 135.2, 209.29948, 135.2, 166.09691, 135.2, 173.89784, 135.2, 200.29836, " +
"370.1, 391.69785, 108.2, 118.698944, 135.2, 181.39537, 135.2, 193.09871, 377.9, 391.6986, 527.0, 544.69824, 54.199997, 87.499214, 108.2, " +
"125.59826, 135.2, 179.2979, 187.7, 211.39763, 135.2, 208.69688, 370.1, 391.69785, 108.2, 120.79874, 135.2, 212.59627, 135.2, 172.69742, 374.0, " +
"391.69824, 524.7, 553.7995, 54.199997, 87.499214, 108.2, 120.79874, 135.2, 216.79735, 135.2, 163.39717, 374.0, 391.69824, 108.2, 121.69865, " +
"135.2, 191.89688, 135.2, 214.39604, 440.6, 468.19724, 517.1, 544.6972";
String[] pointss = points.split(", ");
List<Column> columns = new ArrayList<> ();
for (int i = 0; i < pointss.length; i += 2) {
columns.add(new Column(Float.parseFloat(pointss[i]), Float.parseFloat(pointss[i+1])));
}
List<Column> columnsWithOverlap = columns.stream()
.distinct()
.sorted()
.collect(toList());
System.out.println(columnsWithOverlap);
}
我目前正在阅读以了解有关按契约(Contract)设计/代码契约(Contract)的更多信息。 据我所知,就是写契约(Invariants,Pre and Post conditions)来确保代码
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
我正在处理 spring cloud contract 并且有一个用例,我希望在缺少一些查询参数时响应为 BAD_REQUEST,而在所有必需查询参数都存在时响应为 OK。为了实现这一点,我有不同的契
按契约(Contract)设计的最佳实践是什么? 在大学里,我通过契约(Contract)范式学习了设计 (在OO环境中) 我们已经学习了三种解决问题的方法: 1)全面编程:涵盖其所有可能的异常(ex
从理论上讲,这似乎是避免错误的一个很好的解决方案,但为什么在实践中我们听到的很少呢? 例如,为什么我们不能在 Java 或 .net 上看到对它的更多支持? 最佳答案 我也一直在寻找这个答案。但它似乎
我读到编译器可以在编译时强制执行 dbc。它是怎么做到的? 最佳答案 据我所知,迄今为止最强大的静态DbC语言是Spec# by Microsoft Research .它使用名为 Boogie 的强
我试图让 VS2010 Ultimate with Code Contracts 生成错误而不是警告。 我有这个简单的测试程序: using System.Diagnostics.Contracts;
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
如何关闭对 Linq2Sql 代码的静态检查? 最佳答案 您可以通过使用 [ContractVerification(false)] 标记有问题的类来抑制对静态代码的检查。 如果您生成的类是部分,您可
我正在使用 WCF 制作一个应用程序的原型(prototype),我正在尝试定义一个回调与派生自另一个接口(interface)的接口(interface)签订契约(Contract)。这样做,生成的
我最近在 .Net Rocks 节目 570 ( http://devjourney.com/community/dotnet-rocks-show-570-with-kevin-hazzard/ )
我注意到微软在 .NET 4 中以一种奇怪的方式命名了他们的代码契约(Contract)相关函数。 他们在“require”和“ensure”的末尾添加“s”,所以有Contract.Requires
我对 WCF 比较陌生。但是,我需要创建一个向 Silverlight 和 AJAX 客户端应用程序公开数据的服务。为了实现这一目标,我创建了以下服务作为概念证明: [ServiceContract(
我一直在兜圈子,试图弄清楚这个问题。 我正在尝试选择已结束最近契约(Contract)但仍保留上一份有效契约(Contract)的员工。 例如,一名员工拥有多份契约(Contract)(有些可能是临时
使用合同密钥,有两个函数fetchByKey和lookupByKey,后者允许我处理否定查找。我没有看到针对合同编号执行相同操作的lookup : (Template t) => ContractId
我有一个用于特定 Assets (A、B、...)的合约的 pandas 数据框。每个契约(Contract)都有开始日期、结束日期(包括两者)和日费率(契约(Contract)不能重叠)。我想生成一
我有这个代码: using System; using System.Diagnostics.Contracts; namespace TestCodeContracts { class Pr
我在使用 Flow 时遇到问题,其中给定的已实现 type 通过要求我仅使用在 type 上声明的属性而不是来限制我的对象 API要求我声明所有 type 的属性。 我是 Flow 的新手,所以我可能
我有一个使用 WCF 与后端数据库通信的 Web 应用程序。我已经一切正常,但我想知道将我相当大的服务契约(Contract)分成几个契约(Contract)是否会更好。 就目前而言,服务契约(Con
我想编写单元测试来验证我的方法不接受无效参数。使用 Code Contract 的 Contract.Requires 调用检查参数的有效性。我为什么要测试合约?我认为我的测试是一种方法规范(实际上是
我是一名优秀的程序员,十分优秀!