Implementation of MAP

xiaoxiao2021-03-06  71

I just read Containers, in order to deepen my understanding, I wrote a special MAP specifically for String. I wanted to make the code faster than Hashmap, I didn't expect to be a lot more slower than Hahmap, give the code first, have time to think about how Improve efficiency ----------------------------------- Code --------- ----------------------------------------- Import java.util. *; Class MEMBERS {static int ID = 0; string key; string value; members (string key, string value) {this.key = key; this.value = value; ID = 1; public boolean equals (Object M) {Boolean B = false; IF ((MEMBERS) M) && ((MEMBERS) m) {if (key == ((MEMBERS) m) .key) b = true;} IF ((m instanceof) MEMBERS) && ((MEMBERS) M) .Value)) B = true; return b;} public string toString () {Return "key" key "value" value;} public string getValue () { Return this.Value;}} public class mymap {int SZ = 100; arraylist [] bucket = new arraylist [SZ]; public string put (String key, string value) {// ok string result = null; int index = key .hashcode ()% SZ; IF (index <0) index = -index; if (bucket [index] == null) bucket [index] = new arraylist (); arraylist al = bucket [index]; Listiterator Li = Al .listitera Tor (); Members Mem = New Members (Key, Value); Boolean Find = false; while (li.hasnext ()) {members imbers) li.next (); if (IM.Equals (MEM)) {Result = im .getValue (); li.set (mem); find = true; break;}}}} if (! Find) bucket [index] .add (mem); returnlean contactskey (String Key) {// ok boolean b = false; int index = key.hashcode ()% sz; if (index <0) index = -index; if (bucket [index] == null) Return B; arraylist al = BUCKET [index ]; Listiterator Li = Al.Listiterator (); MEMBERS IKEY3 =

New Members (key, null); while (li.hasnext ()) {met () li.next (); if (iKey2.Equals (iKey3)) B = true;} Return B;} Public Boolean ContaSvalue (String value) {// ok boolean b = false; members IKEY4 = New Members (null, value); for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-91688.html

New Post(0)