1 PackageStudymap;2 3 /**4 * Achieve a simple map5 * Low Efficiency6 */7 ImportJavax.crypto.interfaces.PBEKey;8 9 Public classStudymap {Tenkeyassignments[] Arry =Newkeyassignments[100]; One Private intsize; A - //put () - Public voidput (Object key,object value) { theKeyassignments temp =Newkeyassignments (key,value); -Arry[size] =temp; -size++; - } + - //get () returns the corresponding value according to key + Publicobject get (Object key) { A for(inti = 0; i < size; i++) { at if(Arry[i].key.equals (key)) { - return(Object) arry[i].value; - } - } - return NULL; - } in - //size () to PublicObject size () { + return(Object) size; - } the * //ForEach () $ Public voidForEach () {Panax Notoginseng for(inti = 0; i < size; i++) { -System.out.println (Arry[i].key + ":" +arry[i].value); the } + } A the //a simple Remove method + Public voidRemove (Object key) { - for(inti = 0; i < size; i++) { $ if(Arry[i].key.equals (key)) { $Arry[i].value =NULL; -Arry[i].key =NULL; - } the - }Wuyi } the - Public Static voidMain (string[] args) { WuStudymap EMap =NewStudymap (); -Emap.put ("Niu", "Dragon Boat Festival")); AboutEmap.put ("Son", "Aunt"); $Emap.put (111,222); -System.out.println (Emap.get ("Niu")); -System.out.println (Emap.get (111)); - System.out.println (Emap.size ()); A Emap.foreach (); +Emap.remove ("Niu"); the Emap.foreach (); - $ } the } the the classkeyassignments { the Object key; - Object value; in the Publickeyassignments (Object key,object value) { the Super(); About This. Key =key; the This. Value =value; the } the}
Java implementation Simple Map