java - Hash maps and hashcode that changes, how to tell set that object has changed? -


hashpaps and hash code questions

A POO is more than written, hashode and helping with fraction Comparator is comparable (not shown here)

  package coll.hset; Public class Dat {private string name; Private string data; Private int aa; // Some commercial reasons have not been used in the hashcode and public int hashCode () {int h = 0; If (name! = Null) {h + = name.hashCode (); } If (dat! = Null) {h + = dat.hashCode (); } Returns H; } Public Boolean equivalent (object o) {if (o installation of DAT) {Dat oo = (Dat) o; If (this.name == tap & oo.name! = Null) {return false; } And if (! Name.equals (oo.name)) {return false; } If (this.dat == faucet & oo.dat! = Null) {return false; } And if (! Dat.equals (oo.dat)) {return false; } Back true; } return false; } Public string getName () {return name; } Public Zero Setname (string name) {this.name = name; } Public string getDat () {return dat; } Public Zero Setdate (string data) {this.dat = dat; } Public int getAa () {return AA; } Public Zero SetA (Intestinal AA) {this.aa = aa; }}   

A user AP:

  Package coll.hset; Import java.util.HashSet; Import java.util.Random; Public Class App {Final Static Ent SZ = 2 ^ 8; / ** * Ultimate Argus * / Public Static Wide Men (String [] AGR) {Random RNDM = New Random; // DD [] D2 = new DAT [500] for creating random data during filling date set; // somewhere saved for OS Int here = 0; HashSet & LT; Dat & gt; DAT = New Hashet & lt; Dat & gt; (); // set for (int i = 0; i <10000; i ++) {dd = new Dat (); Dd.setAa (i); // Fill random date and name. Four V = (four) (65 + rndm.nextInt (26)); Dd.setDat ("a" + v); V = (four) (65 + rndm.nextInt (26)); Char v1 = (four) (65 + rndm.nextInt (26)); Four v2 = (four) (65 + rndm.nextInt (26)); Four v3 = (four) (65 + rndm.nextInt (26)); Four v4 = (four) (65 + rndm.nextInt (26)); Dd.setName (v + "+ v1 + v2 + v3 + v1 + v + v4); dats.add (day); if (i% 60 == 0) {d2 [fills ++] = dd;}} DAT Ref = D2 [0]; int hash = hash (ref.hashCode ()); int idx = indexFor (hash, SZ); boolean is 1 = DATA CONCRENSNT (D2 [0]); System.out.println ("Has d2:" + has1 + ", name:" + ref.getName () + ", hash:" + ref.hashCode () + ". Hash2:" + hash + ", idx:" + idx + " , When the size of the table: "+ SZ); D2 [0] .setName (ref.getName () +" l "); // d2 [0] .setName (ref.getName (+ +" l "); D2 [0] .setName ("Tony G"); // ref.setDat ("sd ="); hash = hash (ref.hashCode ()); // If you run it several times you will see that something The table is similar in cases, so a quickness Instead of removing, extracting and adding back after I change I am after idx = indexFor (hash, SZ); Has1 = dats.contains (D2 [0]); System.out.println ("after the name change D: "+ has1 +", name: "+ ref.getName () +". "); System.out.println (" has d2: "+ has1 +", name: "+ ref.getName () + ", Hash:" + ref.hashCode () + ". Hash2:" + hash + ", idx:" + idx + ", when the size of the table:" + SZ); System.out.println ("at:" + new java.util.Date ()); } Sun Java effect from static hash {//} / This function ensures that every bit position rotates in different hashcode than // continuous multiplication // number of collisions (approximately 8 default The factor is on the load). * / H ^ = (H & gt; & gt;> 20) ^ (H & gt; & gt; & gt; 12); Returns H ^ (H>>> 7) ^ (H>> 4); } Fixed Engineer (Int h, int lamp) {Return H & A. (Length - 1); }}   

As the second finding of hope says that the DT object D2 [0] is not in the set, it is also thought that it is. I know how to fix it - the only way - remove it, change it and then add it back. Is there any other way to tell if there is a particular object to change?

From

You can see how Oracle / Sun Java Hashmap has a question again - can we add a new method that tells the set - Re-share it, instead of removing it and adding it, its more efficient

If you run above the code, many times it will look that in some cases the table is the same (for the mutated object before and after the hash code), so instead of removing a quick reshash And after adding it after the change, whatever I am, takes advantage of this fact and only if the bucket change

The object has been assumed to be stable during the lifetime of the hash object, your question is so strict The answer is: no. When you modify your object, as if the hash code is changed, you will remove it from the map better and add it back again.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -