- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
奇怪的是,AbstractList::equals()
的默认 JDK 6 实现 似乎没有首先检查两个列表是否具有相同的大小:
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
ListIterator<E> e1 = listIterator();
ListIterator e2 = ((List) o).listIterator();
while(e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
如果两个列表都包含很多项目,或者需要时间比较的项目,它将在意识到一个列表比另一个短之前将它们全部比较;在我看来,这真的很低效,因为甚至可以在不进行比较的情况下实现平等。
特别是在很多情况下,列表的大小在大多数情况下会有所不同。此外,大多数 Java List
实现具有 O(1) size()
性能(即使是 LinkedList,它在缓存中保持其大小)。
这种默认实现有充分的理由吗?
最佳答案
The operation of the equals method is specified in some detail, and it requires the O(n) behavior. While this may be suboptimal for subclasses whose size method is O(1), for some subclasses the size method may itself be O(n) and the requested behavior would actually be a degradation. In any event the spec is clear and this change cannot be made.
Note that a subclass may override equals if desired, inserting a size comparison when appropriate.
关于java - AbstractList::equals() 的 JDK 实现不会首先检查列表大小是否相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18958113/
我对第一个问题的直觉是肯定的。对于第二个问题,我在工作中确实看到,有人使用 JDK8 和 ANT,然后将用 JDK6 编写的旧包编译到 1.6。我真的很困惑。 最佳答案 一般来说,java 向后兼容所
据我所知,在windows中使用JDK有两种方式: 下载JDK安装文件并安装。 下载 JDK 二进制文件。 它们有什么区别? 最佳答案 优点:简单易行,突然间一切正常。 缺点:现在一切都使用新版本 -
我正在安装 HANA Studio,并且已下载 JDK 1.8 和 JDK 1.7。我将 JDK 1.8 用于 Eclipse 和我正在处理的其他一些事情,但是当我尝试通过 SAP HANA 生命周期
JDK 7 的哪些特性(不包括 invokedynamic,因为它不被 java 使用)导致新的类文件版本与 JDK 6 不兼容。似乎所有特性都可以通过编译器生成胶水代码来实现。例如 switch 语
在redhat机器上安装cloudera的库来创建cloudera集群是否必须使用Oracle JDK而不是Open JDK? 最佳答案 在撰写本文时,只有 Oracle JDK 版本经过认证可与 C
下面的语句在 Java 7 中有效吗? Timestamp.valueOf("0000-00-00 00:00:00.000000"); 因为使用 JDK 1.6 构建上述代码效果很好,但在使用 JD
更新 在整个评论中,结果证明我采用的基准测试方法是不正确的,因此结果具有误导性。纠正我的方法后(如已接受的答案),结果正如人们所期望的 - JDK 13 的性能与 JDK 11 一样好。有关更多详细信
我们很快就会从 jdk14 迁移到 jdk16。我们的是桌面应用程序。我需要采取什么措施来确保它在客户端机器上正常工作?现在他们中的一些人使用 JRE4 和一些 JRE6.Server-Solaris
我在/usr/lib/jvm 中有 jdk1.7.0 目录以及其他 open-jdk 版本。我希望我的 Ubuntu 12.04 将此 jdk(jdk1.7.0) 视为其主要 jdk,即我不想使用 o
我认为这可能与 Why does a generic cast of a List to List succeed on Sun JDK 6 but fail to compile on Oracle
代码使用 JDK 8 (1.8.0_212) 编译良好,但使用 JDK 11 (11.0.3) Oracle jdk 和 open jdk (aws corretto) 编译失败 尝试使用 javac
是否可以在 cygwin 上安装任何版本的 Sun JDK 或 Open JDK。 我寻找此选项的原因是:有许多工具(例如 jStack、jMap)在 JDK 的 unix 版本中可用,但在 wind
请确认以上说法? 当他们提到 JDK 时,我需要知道他们指的是什么。 最佳答案 Java Development Kit 是我们通常指的一组创建 Java 应用程序的工具,包括 Java Compil
使用 java -version 给我这个。 java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
这个问题在这里已经有了答案: JAVA_HOME should point to a JDK not a JRE (25 个答案) 关闭 4 年前。 您好,感谢您提供的任何帮助。 我刚刚升级到 Ub
没错,自阿里、腾讯之后,华为也终于开源了自家的 JDK——毕昇 JDK! 免费!免费!免费!!! Oracle 要慌了? 毕昇 JDK 毕昇 JDK 是华为内部 OpenJDK 定制版 Hu
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭去年。 Improve this quest
将 Arquillian 添加到 Maven 构建时,我在 Eclipse 中遇到上述异常: Missing artifact sun.jdk:jconsole:jar:jdk
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我正在尝试创建一个 pom,它将: 使用 maven-toolchains-plugin 中的正确 JDK基于 java.version 属性。 根据 maven-toolchains-plugin
我是一名优秀的程序员,十分优秀!