Eclipse implements a function hashCode()for a single Node class as follows:
hashCode()
class Node{ int val; Node next; public Node(int val){ this.val = val; next = null; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((next == null) ? 0 : next.hashCode()); result = prime * result + val; return result; } }
Now hashCode()for Node it depends on the hash code of subsequent nodes.
Thus, each call hashCode()will take a weighted linear time in the length of the linked list. Thus, use HashSet<Node>will become impracticable.
HashSet<Node>
One way around this is to cache the value hashCodein the variable (call the hash) so that it is evaluated only once. But even in this case, the hash will become invalid after changing any Node value. And again, linear time is required to change the hashCodenodes that follow the current node.
hashCode
, Node?
, : LinkedList do? , , LinkedList.Node hashCode() equals() ( ).
LinkedList
LinkedList.Node
equals()
? , , . - , -. .
:
, HashSet<Node> .
, . . ( ), HashSet.
HashSet
LinkedList.Node . - , ( ), , LinkedList - . AbstractList ( ).
AbstractList
OpenJDK, , Oracle JDKs
, . , , . , , 5 . , . , , , , 1, - , . int, . , , .
, (, ) . , , , .; -)
: java.util.HashSet( , HashMap, ) , . , , HashSet, , , .
Source: https://habr.com/ru/post/1536458/More articles:https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1536453/best-mysql-datasource-properties-for-grails-tomcat-jndi&usg=ALkJrhiVdKqY66gwFRVu5KGGb7G2ZB6dFgJavaFX - horizontal label text - javawhy can’t the behavior be dynamic? - backbone.js"Cannot autoload constants" using rspec but not rails - ruby-on-railsnumpy.memmap map to save file - pythonJenkins runs parallel scripts - jenkinsConditionally remove header / footer in Magento - phpPostgres и c3p0 неверная ошибка часового пояса во время соединения - postgresqlHow to add html in response to a site before it reaches a Java browser? - javaПоследовательный способ проверить, является ли np.array datetime-like - numpyAll Articles