-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathListMap.java
282 lines (207 loc) · 5.64 KB
/
ListMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package org.hy.common;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Map与List的融合体。即有关键字找值的功能,又有按编号找值的功能。
*
* Map继承Hashtable,有同步功能。
*
* @author ZhengWei(HY)
* @version v1.0
* v2.0 2014-10-17 添加isSafe属性
* 添加getKeys()方法
* 添加getKey(...)方法
* v3.0 2023-02-01 添加getValues()方法(有顺序的)
* @createDate 2012-07-25
*/
public class ListMap<K ,V> extends Hashtable<K ,V> implements Map<K ,V>
{
private static final long serialVersionUID = -5793511506137811113L;
private List<K> keyList;
/** 是否安全。主要指针 getKeys() 方法。默认是安全的,但性能略略略慢 */
private boolean isSafe;
public ListMap()
{
this(11 ,true);
}
public ListMap(int i_InitialCapacity)
{
this(i_InitialCapacity ,true);
}
public ListMap(boolean i_IsSafe)
{
this(11 ,i_IsSafe);
}
public ListMap(int i_InitialCapacity ,boolean i_IsSafe)
{
super(i_InitialCapacity);
this.isSafe = i_IsSafe;
this.keyList = new ArrayList<K>();
}
@Override
public synchronized V put(K key ,V value)
{
V v_Ret = null;
if ( this.containsKey(key) )
{
v_Ret = super.put(key, value);
}
else
{
v_Ret = super.put(key, value);
this.keyList.add(key);
}
return v_Ret;
}
@Override
public synchronized void putAll(Map<? extends K, ? extends V> t)
{
Iterator<? extends Map.Entry<? extends K, ? extends V>> i = t.entrySet().iterator();
while (i.hasNext())
{
Map.Entry<? extends K, ? extends V> e = i.next();
put(e.getKey(), e.getValue());
}
}
@Override
public synchronized V remove(Object key)
{
V v_Ret = super.remove(key);
if ( v_Ret != null )
{
try
{
this.keyList.remove(key);
}
catch (Exception e)
{
e.printStackTrace();
}
return v_Ret;
}
else
{
return null;
}
}
/**
* 按索引号删除元素
*
* @param i_Index 索引号
* @return
*/
public synchronized V remove(int i_Index)
{
return this.remove(this.keyList.get(i_Index));
}
@Override
public synchronized void clear()
{
super.clear();
try
{
this.keyList.clear();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 按索引号获取对象值
*
* @param i_Index
* @return
*/
public synchronized V get(int i_Index)
{
if ( i_Index < 0 || i_Index >= this.keyList.size() )
{
throw new ArrayIndexOutOfBoundsException("Index[" + i_Index + "] is out of Bounds. Max size is " + this.keyList.size() + ".");
}
return this.get(this.keyList.get(i_Index));
}
/**
* 按索引号获取关键字
*
* @param i_Index
* @return
*/
public synchronized K getKey(int i_Index)
{
if ( i_Index < 0 || i_Index >= this.keyList.size() )
{
throw new ArrayIndexOutOfBoundsException("Index[" + i_Index + "] is out of Bounds. Max size is " + this.keyList.size() + ".");
}
return this.keyList.get(i_Index);
}
/**
* 获取关键字所在的索引号
*
* @param i_Key
* @return
*/
public synchronized int getIndex(K i_Key)
{
return this.keyList.indexOf(i_Key);
}
/**
* 获取所有Key值
*
* @return
*/
public synchronized List<K> getKeys()
{
if ( this.isSafe )
{
if ( Help.isNull(this.keyList) )
{
return null;
}
List<K> v_Ret = new ArrayList<K>(this.keyList.size());
v_Ret.addAll(this.keyList);
return v_Ret;
}
else
{
return this.keyList;
}
}
/**
* 按排序获取所有Value值
*
* @author ZhengWei(HY)
* @createDate 2023-02-01
* @version v1.0
*
* @return
*/
public synchronized List<V> getValues()
{
if ( Help.isNull(this.keyList) )
{
return null;
}
List<V> v_Ret = new ArrayList<V>(this.keyList.size());
for (K v_Key : this.keyList)
{
v_Ret.add(this.get(v_Key));
}
return v_Ret;
}
/*
ZhengWei(HY) Del 2016-07-30
不能实现这个方法。首先JDK中的Hashtable、ArrayList中也没有实现此方法。
它会在元素还有用,但集合对象本身没有用时,释放元素对象
一些与finalize相关的方法,由于一些致命的缺陷,已经被废弃了
protected void finalize() throws Throwable
{
this.clear();
this.keyList = null;
super.finalize();
}
*/
}