Fastbyidmap is a hash-based, linear probe rather than a detached link when dealing with collisions, thus saving memory overhead by not having to add an Map.entry object for each entry.
The following code is a demo of a linear probe map:
Package com.example.mahout; Public classArrayhashst_linear_probing<key, val> {Private intM =30001;PrivateKey[] keys = (key[])NewOBJECT[M];PrivateVal[] Vals = (val[])NewOBJECT[M];Private int Hash(Key key) {return(Key.hashcode () &0x7fffffff)% M; } Public void put(Key key, Val val) {intI for(i = hash (key); Keys[i]! =NULL; i = (i +1) (% M)if(Keys[i].equals (key)) Break; Keys[i] = key; Vals[i] = val; } PublicValGet(Key key) {intI for(i = hash (key); Keys[i]! =NULL; i = (i +1) (% M)if(Keys[i].equals (key)) Break;returnVals[i]; } Public Static void Main(string[] args) {Arrayhashst_linear_probing<string,string> St =NewArrayhashst_linear_probing<string, string> (); St.put ("Jocularly","Jocularly"); St.put ("Seriously","Seriously"); St.put ("Listen","Listen"); St.put ("Suburban","Suburban"); St.put ("untravelled","untravelled"); St.put ("Considerating","Considerating"); St.put ("Browsing","Browsing"); System. out. println (St.Get("Jocularly")); }}
This is the demo of the Detach Link:
Package com.example.mahout; Public classListhashst_separate_chaining<key, value> {Private intM =8191;Privatenode[] St =NewNODE[M];Private Static classNode {Object key; Object Val; Node Next; Node (object key, Object Val, node next) { This. key = key; This. val = val; This. next = Next; } }Private int Hash(Key key) {return(Key.hashcode () &0x7fffffff)% M; } Public void put(Key key, Value Val) {inti = hash (key); for(Node x = st[i]; x! =NULL; x = X.next) {if(Key.equals (X.key)) {x.val = val;return; }} St[i] =NewNode (Key, Val, St[i]); } PublicValueGet(Key key) {inti = hash (key);//system.out.println (i); for(Node x = st[i]; x! =NULL; x = x.next) {System. out. println (X.val);if(Key.equals (X.key))return(Value) X.val; }return NULL; } Public Static void Main(string[] args) {//TODO auto-generated method stubListhashst_separate_chaining<string, String> st =NewListhashst_separate_chaining<string, string> (); St.put ("Jocularly","Jocularly"); St.put ("Seriously","Seriously"); St.put ("Listen","Listen"); St.put ("Suburban","Suburban"); St.put ("untravelled","untravelled"); St.put ("Considerating","Considerating"); St.put ("Browsing","Browsing"); St.Get("Jocularly");//system.out.println (St.get ("jocularly"));}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Mahout-hashmap's evolutionary version of Fastbyidmap