首页 > 代码库 > java实现简单map

java实现简单map

 1 package StudyMap; 2  3 /** 4  * 实现简单的map 5  * 效率低 6  */ 7 import javax.crypto.interfaces.PBEKey; 8  9 public class StudyMap {10     keyAssignments[] arry = new keyAssignments[100];11     private int size;12     13     // put()14     public void put( Object key,Object value ) {15         keyAssignments temp = new keyAssignments( key,value);16         arry[size] = temp;17         size++;18     }19     20     // get() 根据key返回对应的value21     public Object get( Object key ) {22         for ( int i = 0; i < size; i++) {23             if ( arry[i].key.equals( key)) {24                 return ( Object) arry[i].value;25             }26         }27         return null;28     }29     30     // size()31     public Object size() {32         return ( Object) size;33     }34     35     // forEach()36     public void forEach() {37         for ( int i = 0; i < size; i++) {38             System.out.println( arry[i].key + ":" + arry[i].value);39         }40     }41     42     // 简单的remove方法43     public void remove( Object key ) {44         for ( int i = 0; i < size; i++) {45             if ( arry[i].key.equals( key)) {46                 arry[i].value = http://www.mamicode.com/null;47                 arry[i].key = null;48             }49             50         }51     }52     53     public static void main( String[] args ) {54         StudyMap eMap = new StudyMap();55         eMap.put( "妞妞","端午");56         eMap.put( "过儿","姑姑");57         eMap.put( 111,222);58         System.out.println( eMap.get( "妞妞"));59         System.out.println( eMap.get( 111));60         System.out.println( eMap.size());61         eMap.forEach();62         eMap.remove( "妞妞");63         eMap.forEach();64         65     }66 }67 68 class keyAssignments {69     Object key;70     Object value;71     72     public keyAssignments( Object key,Object value ) {73         super();74         this.key = key;75         this.value =http://www.mamicode.com/ value;76     }77 }

 

java实现简单map