当.equals()无法正确设置时怎么办?
我正在尝试使用优秀的 JGraphT 库用 Java 编写 Scrabble 程序,作为有向无环图和 Java 的练习。
所以,我的边将是字母,顶点位集是字母表的大小。本质上,您一个字母一个字母地遍历图表,并检查您所在的位集,看看哪些字母附加到从根节点开始的字母弧上后会形成单词。
我明白了,但让我担心的是 JGraphT Javadoc 中的内容:
此方法使用该图的 EdgeFactory 创建新边 e。对于要添加的新边,e 不得等于图形中的任何其他边(即使图形允许边重数)。更正式地说,该图不得包含任何使 e2.equals(e) 的边 e2。如果找到这样的 e2,则放弃新创建的边 e,该方法保持该图不变,返回 null。
我的边和节点永远不会是唯一的,除非引用不匹配。那么,我的问题是 Java 程序员在这里会做什么?
创建一个 Letter 类和一个 BitSet 类,并将 equals() 保留为默认值,因为引用不匹配,这将始终为 false?但是,那么我如何处理所有依赖 .equals() 正确的其他方法,如 .contains()?
创建 Edge 和 Node 类作为真实 Letter 和 BitSet 类的薄包装,并将始终为 false 的 .equals() 放入 Edge;Node 中,将真实的 .equals() 放入 Letter;Bitset 中?
公共类边缘{ 私人信件; //getter 和 setter 即将到来 公共布尔等于(对象b){ 返回假; } }
- 还有别的事吗?
I'm trying to use the excellent JGraphT library to write a Scrabble program in Java as practice with directed acyclic graphs and Java.
So, my edges are going to be letters and the vertices bitsets the size of the alphabet. Essentially, you traverse the graph letter by letter and check the bitset you're at to see what letters form a word if appended to the arc of letters you've followed from the root node.
I get that, but what worries me is this from the JGraphT Javadoc:
This method creates the new edge e using this graph's EdgeFactory. For the new edge to be added e must not be equal to any other edge the graph (even if the graph allows edge-multiplicity). More formally, the graph must not contain any edge e2 such that e2.equals(e). If such e2 is found then the newly created edge e is abandoned, the method leaves this graph unchanged returns null.
My edges and nodes will never be unique except in the sense that references don't match. So, my question is what would a Java programmer do here?
Create a Letter class and a BitSet class and leave the equals() to default which will always be false since the references won't match? But, then how do I handle all those other methods that depend on .equals() being correct like .contains()?
Create Edge and Node classes as thin wrappers around the real Letter and BitSet classes and put the always false .equals() in Edge;Node and the real one in Letter;Bitset?
public class Edge { private Letter letter; //getter and setter coming public boolean equals (Object b) { return false; } }
- Something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顶点已经具有由 BitSet 中设置的位定义的标识,因此您可以将 BitSet 本身用于顶点。
一条边通常应该携带有关其开始和结束的顶点的信息,因此我建议使用一个
Edge
类,其中包含开始BitSet
和结束BitSet
> 和边缘字母
。如果两个顶点之间不存在两条边(即不允许存在边重数),那么您可以在 Edge 类中定义 equals 和 hashcode 作为测试开始和结束的相等性,忽略字母。如果边的字母很重要,因为您可能有多个具有相同起点和终点的边,则需要节点之间的字母适当相等。
The vertices already have an identity defined by the bits set in the BitSet, so you can use the
BitSet
itself for the vertices.An edge should normally carry information about the vertices it starts and ends in, so I suggest an
Edge
class which contains the startBitSet
, the endBitSet
, and the edgeLetter
.If no two edges exist between two vertices (i.e. edge-multiplicity is disallowed), then you can define equals and hashcode in the
Edge
class as testing for equality of start and end, ignoring the letter. If the letter of an edge is important because you might have multiple edges with equal start and end, you need a proper equality of letters between nodes.