这是一个二维表的容器。包装如下形式的表格:
(这blog的编辑器什么都好,怎么没有表格呢?)
日期 |
商品 |
数量 |
金额 |
合计利润 |
040901 |
|
|
|
|
|
P001 |
55 |
5401.00 |
3705.90 |
|
P002 |
48 |
4200.00 |
840.00 |
|
P003 |
50 |
500.00 |
416.00 |
|
P004 |
0 |
0.00 |
0.00 |
040904 |
|
|
|
|
|
P001 |
0 |
0.00 |
0.00 |
|
P002 |
35 |
3062.50 |
612.50 |
|
P003 |
68 |
680.00 |
565.76 |
|
P004 |
52 |
624.00 |
176.80 |
040908 |
|
|
|
|
|
P001 |
98 |
9623.6 |
6603.24 |
|
P002 |
0 |
0.00 |
0.00 |
|
P003 |
78 |
780.00 |
648.96 |
|
P004 |
10 |
120.00 |
34.00 |
040910 |
|
|
|
|
|
P001 |
65 |
6383.00 |
4379.70 |
|
P002 |
35 |
3062.50 |
612.50 |
|
P003 |
80 |
800.00 |
665.60 |
|
P004 |
25 |
300.00 |
85.00 |
040913 |
|
|
|
|
|
P001 |
0 |
0.00 |
0.00 |
|
P002 |
56 |
4900.00 |
980.00 |
|
P003 |
38 |
380.00 |
316.16 |
|
P004 |
40 |
480.00 |
136.00 |
为了取得容器中的entity,我们还需要一个辅助类TwoDimensionTableEntity(熟悉Map的一定知道Map.Entity吧

)
好了现在看一下实现:
1
package org.mstar.collection;
2
3
/**//**
4
* @author mty
5
*/
6
public class TwoDimensionTableEntity
{
7
private Object d1Key;
8
9
private Object d2Key;
10
11
private Object value;
12
13
/**//**
14
*
15
*/
16
public TwoDimensionTableEntity()
{
17
super();
18
}
19
20
/**//**
21
* @param key
22
* @param key2
23
* @param value
24
*/
25
public TwoDimensionTableEntity(Object key, Object key2, Object value)
{
26
super();
27
d1Key = key;
28
d2Key = key2;
29
this.value = value;
30
}
31
32
/**//**
33
* @return Returns the d1Key.
34
*/
35
public Object getD1Key()
{
36
return d1Key;
37
}
38
39
/**//**
40
* @param key
41
* The d1Key to set.
42
*/
43
public void setD1Key(Object key)
{
44
d1Key = key;
45
}
46
47
/**//**
48
* @return Returns the d2Key.
49
*/
50
public Object getD2Key()
{
51
return d2Key;
52
}
53
54
/**//**
55
* @param key
56
* The d2Key to set.
57
*/
58
public void setD2Key(Object key)
{
59
d2Key = key;
60
}
61
62
/**//**
63
* @return Returns the value.
64
*/
65
public Object getValue()
{
66
return value;
67
}
68
69
/**//**
70
* @param value
71
* The value to set.
72
*/
73
public void setValue(Object value)
{
74
this.value = value;
75
}
76
77
public String toString()
{
78
return d1Key + "\t" + d2Key + "\t" + value;
79
}
80
}
1
package org.mstar.collection;
2
3
import java.util.*;
4
5
/**//**
6
* @author mty
7
*/
8
public class TwoDimensionTable
{
9
10
private Map d1Map;
11
12
public TwoDimensionTable()
{
13
d1Map = new HashMap();
14
}
15
16
public TwoDimensionTable(Object[] d1Keys, Map[] d2Entities)
{
17
d1Map = new HashMap();
18
int d1Size = d1Keys.length > d2Entities.length ? d2Entities.length
19
: d1Keys.length;//如果两个length不一样,取小者为一维size
20
for (int i = 0; i < d1Size; i++)
{
21
d1Map.put(d1Keys[i], d2Entities[i]);
22
}
23
}
24
25
public void add(Object d1Key, Object d2Key, Object value)
{
26
if (d1Map.containsKey(d1Key))
{
27
Map d2Map = (Map) d1Map.get(d1Key);
28
d2Map.put(d2Key, value);
29
} else
{
30
Map d2Map = new HashMap();
31
d2Map.put(d2Key, value);
32
d1Map.put(d1Key, d2Map);
33
}
34
}
35
36
public void add(Object d1Key,Map d2Map)
{
37
d1Map.put(d1Key,d2Map);
38
}
39
40
public void remove(Object d1Key, Object d2Key)
{
41
if (d1Map.containsKey(d1Key))
{
42
Map d2Map = (Map) d1Map.get(d1Key);
43
d2Map.remove(d2Key);
44
if (d2Map.size() == 0)
{
45
d1Map.remove(d1Key);
46
}
47
}
48
}
49
50
public void remove(Object d1Key)
{
51
d1Map.remove(d1Key);
52
}
53
54
public Object get(Object d1Key, Object d2Key)
{
55
if (d1Map.containsKey(d1Key))
{
56
return ((Map) d1Map.get(d1Key)).get(d2Key);
57
} else
{
58
return null;
59
}
60
}
61
62
public Map get(Object d1Key)
{
63
return (Map) d1Map.get(d1Key);
64
}
65
66
public int d1Size()
{
67
return d1Map.size();
68
}
69
70
public int d2Size(Object d1Key)
{
71
if (d1Map.containsKey(d1Key))
{
72
return ((Map) d1Map.get(d1Key)).size();
73
} else
{
74
return 0;
75
}
76
}
77
78
public Set d1KeySet()
{
79
return d1Map.keySet();
80
}
81
82
public Set d2KeySet(Object d1Key)
{
83
if (d1Map.containsKey(d1Key))
{
84
return ((Map) d1Map.get(d1Key)).keySet();
85
} else
{
86
return new HashSet();
87
}
88
}
89
90
public Collection d2Values(Object d1Key)
{
91
if (d1Map.containsKey(d1Key))
{
92
return ((Map) d1Map.get(d1Key)).values();
93
} else
{
94
return new ArrayList();
95
}
96
}
97
98
public List entityList()
{
99
List result = new ArrayList();
100
Set d1KeySet = this.d1KeySet();
101
Iterator iter1 = d1KeySet.iterator();
102
while (iter1.hasNext())
{
103
Object d1Key = iter1.next();
104
Map d2Map = (Map) d1Map.get(d1Key);
105
Set d2KeySet = d2Map.keySet();
106
Iterator iter2 = d2KeySet.iterator();
107
while (iter2.hasNext())
{
108
Object d2Key = iter2.next();
109
Object value = d2Map.get(d2Key);
110
TwoDimensionTableEntity entity = new TwoDimensionTableEntity(
111
d1Key, d2Key, value);
112
result.add(entity);
113
}
114
}
115
return result;
116
}
117
} 这个类应该还有很多需要改进的地方,请大家提建议!