Map-采用自定义的ArrayList实现

这里主要验证前面的手写CustomerArrayList是否正确。

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 CustomerMap2<Key, Value> implements CustomerMap<Key, Value> {
 2 
 3     private CustomerList<Entity<Key, Value>> MapList = new CustomerArrayList<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(int i = 0; i < MapList.size(); i++) {
49             Entity entity = (Entity) MapList.get(i);
50             System.out.println("key: " + entity.getKey() + "  value: " + entity.getValue() );
51         }
52     }
53     
54     private Entity<Key, Value> getEntity(Key key) {
55         
56         for(int i = 0; i < MapList.size(); i++) {
57             Entity entity = (Entity) MapList.get(i);
58             if(entity.getKey().equals(key)) {
59                 return entity;
60             }
61         }
62         return null;
63     }
64 
65 }

测试代码:

 1 public class ArrayListMap2Test {
 2 
 3     @Test
 4     public void testPut001() {
 5         
 6         CustomerMap<String, String> map = new CustomerMap2<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 CustomerMap2<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 CustomerMap2<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 CustomerMap2<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 CustomerMap2<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     
72 }

运行截图:

 

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