Map-采用jdk的List实现

实现思路:

1.这个比较简单 利用ArrayList实现。

git上项目路径: https://github.com/0ziyu0/handWriting

代码

接口:

 1 public interface CustomerMap<Key, Value> {
 2 
 3     void put(Key key, Value value);
 4     
 5     Value get(Key key);
 6     
 7     void remove(Key key);
 8     
 9     Integer size();
10     
11     void print();
12 }

实现:

 1 public class CustomerArrayListMap<Key, Value> implements CustomerMap<Key, Value> {
 2 
 3     private List<Entity<Key, Value>> MapList = new ArrayList<Entity<Key, Value>>();
 4 
 5     @Override
 6     public void put(Key key, Value value) {
 7         
 8         Entity<Key, Value> entity = getEntity(key);
 9         if(entity != null) {
10             entity.setValue(value);
11         } else {
12             Entity<Key, Value> newEntity = new Entity<Key, Value>(key, value);
13             MapList.add(newEntity);
14         }
15         
16     }
17 
18     @Override
19     public Value get(Key key) {
20 
21         Entity<Key, Value> entity = getEntity(key);
22         if(entity != null) {
23             return entity.getValue();
24         }
25         
26         return null;
27     }
28 
29     @Override
30     public void remove(Key key) {
31         
32         Entity<Key, Value> entity = getEntity(key);
33         if(entity != null) {
34             MapList.remove(entity);
35         }
36         
37     }
38 
39     @Override
40     public Integer size() {
41         
42         return MapList.size();
43     }
44 
45     @Override
46     public void print() {
47         System.out.println("size: " + MapList.size());
48         for (Entity<Key, Value> entity : MapList) {
49             System.out.println("key: " + entity.getKey() + "  value: " + entity.getValue() );
50         }
51     }
52     
53     private Entity<Key, Value> getEntity(Key key) {
54         
55         for (Entity<Key, Value> entity : MapList) {
56             if(entity.getKey().equals(key)) {
57                 return entity;
58             }
59         }
60         return null;
61     }
62 
63 }
64 
65 class Entity<Key, Value> {
66 
67     private Key key;
68     private Value value;
69     public Entity(Key key, Value value) {
70         super();
71         this.key = key;
72         this.value = value;
73     }
74     public Key getKey() {
75         return key;
76     }
77     public void setKey(Key key) {
78         this.key = key;
79     }
80     public Value getValue() {
81         return value;
82     }
83     public void setValue(Value value) {
84         this.value = value;
85     }
86 
87 }

测试代码:

 1 public class ArrayListMapTest {
 2 
 3     @Test
 4     public void testPut001() {
 5         
 6         CustomerMap<String, String> map = new CustomerArrayListMap<String, String>();
 7         map.put("1", "001");
 8         map.print();
 9         
10     }
11     
12     @Test
13     public void testPut002() {
14         
15         CustomerMap<String, String> map = new CustomerArrayListMap<String, String>();
16         map.put("1", "001");
17         map.put("2", "002");
18         map.put("1", "001");
19         map.put("3", "001");
20         map.print();
21         
22     }
23     
24     @Test
25     public void testGet001() {
26         
27         CustomerMap<String, String> map = new CustomerArrayListMap<String, String>();
28         map.put("1", "001");
29         map.put("2", "002");
30         map.put("3", "001");
31         
32         System.out.println(map.get("1"));
33         System.out.println(map.get(null));
34         System.out.println(map.get("2"));
35         System.out.println(map.get("4"));
36         
37     }
38     
39     @Test
40     public void testRemove001() {
41         
42         CustomerMap<String, String> map = new CustomerArrayListMap<String, String>();
43         map.put("1", "001");
44         map.put("2", "002");
45         map.put("3", "003");
46         
47         map.print();
48         System.out.println("==========");
49         
50         map.remove("1");
51         map.print();
52         
53     }
54     
55     @Test
56     public void testRemove002() {
57         
58         CustomerMap<String, String> map = new CustomerArrayListMap<String, String>();
59         map.put("1", "001");
60         map.put("2", "002");
61         map.put("3", "003");
62         
63         map.print();
64         System.out.println("==========");
65         
66         map.remove("4");
67         map.print();
68         
69         
70     }
71 }

运行截图:

 

posted @ 2019-01-16 21:17  东隅已逝x  Views(100)  Comments(0)    收藏  举报