Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behavior while using HashSet
    primarykey
    data
    text
    <p>I'm using a HashSet to store objects of a new type that I wrote. The type definition is of the following sort: </p> <pre><code>class Node{ Node arr[] = new Node[5]; boolean flag = false; } </code></pre> <p>I rewrote hashCode as follows:</p> <pre><code>int hash = Arrays.deepHashCode(arr); if(flag==true) return hash; else return -hash; </code></pre> <p>and equals:</p> <pre><code>public boolean equals(Object other){ Node t = (Node) other; return Arrays.deepEquals(arr, t.arr)&amp;&amp;(flag==t.flag); } </code></pre> <p>I'm adding words into the nodes (the nodes are trie nodes) and I'm doing so in dictionary order. I'm then storing the nodes into the hashset. The strange thing is that while the hashset works fine for words beginning with one letter, as soon as I get to words beginning with the next letter, my code gets stuck. For example, everything works fine for words beginning with 'a', but as soon as it gets to words beginning with 'b', it gets stuck. Same for 'c' and 'd' and so on.</p> <p>I narrowed it down to one line of code: the line where I'm adding the node to the hashset. </p> <p>Since I didn't write the hashset, I don't know what's going on here. I'm sure that that's where the code gets stuck (it doesn't crash. It seems to loop infinitely, but I'm not sure). Does anyone know what's going on?</p> <p>--Edit--</p> <p>Lots of head scratching and many print statements later, I have determined that the graph I was making was not actually a DAG, even though it should be and therefore, the deepHashCode and deepEquals functions were being thrown off. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload