在oracle中的PriorityQueue类描述中,特别是关于方法add,页面说到add的返回值:
返回: true(由 Collection.add(E) 指定)
是不是说这个方法总是返回true?
提前致谢!
类(class)描述链接:https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
最佳答案
如果您单击文档中的链接,您将阅读 Collection.add
的内容说到返回值:
Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)
因此返回值的约定是它必须始终返回 true except 如果:
- 该集合不允许重复
- 元素已经存在
PriorityQueue
是否允许重复?是的。因此它永远不会返回 false
。
关于java - PriorityQueue<E> 类的方法添加可以返回 false 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57586713/