码迷,mamicode.com
首页 > 编程语言 > 详细

java实现简单map

时间:2014-11-26 01:07:51      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   sp   java   for   div   log   

 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返回对应的value
21     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 = 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 = value;
76     }
77 }

 

java实现简单map

标签:style   blog   ar   color   sp   java   for   div   log   

原文地址:http://www.cnblogs.com/Ouyangan/p/4122164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!