Skip to content

Commit 6456b18

Browse files
committed
优化 ListMap
1 parent 122e262 commit 6456b18

2 files changed

Lines changed: 120 additions & 94 deletions

File tree

okhttps/src/main/java/com/ejlchina/okhttps/ListMap.java

Lines changed: 118 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,14 @@
1313
*/
1414
public class ListMap<V> extends AbstractMap<String, V> {
1515

16-
transient final List<String> keys;
17-
18-
transient final List<V> values;
19-
20-
transient Set<Entry<String, V>> entrySet;
16+
transient final List<Entry<String, V>> entries;
2117

2218
public ListMap() {
2319
this(0);
2420
}
2521

2622
public ListMap(int initSize) {
27-
keys = new ArrayList<>(initSize);
28-
values = new ArrayList<>(initSize);
29-
}
30-
31-
static class Itr<V> implements Iterator<Entry<String, V>> {
32-
33-
final Iterator<String> kit;
34-
final Iterator<V> vit;
35-
36-
public Itr(Iterator<String> kit, Iterator<V> vit) {
37-
this.kit = kit;
38-
this.vit = vit;
39-
}
40-
41-
@Override
42-
public boolean hasNext() {
43-
return kit.hasNext() && vit.hasNext();
44-
}
45-
46-
@Override
47-
public Entry<String, V> next() {
48-
return new SimpleEntry<>(kit.next(), vit.next());
49-
}
50-
51-
@Override
52-
public void remove() {
53-
kit.remove();
54-
vit.remove();
55-
}
56-
23+
entries = new ArrayList<>(initSize);
5724
}
5825

5926
/**
@@ -63,16 +30,18 @@ class EntrySet extends AbstractSet<Entry<String, V>> {
6330

6431
@Override
6532
public Iterator<Entry<String, V>> iterator() {
66-
return new Itr<>(keys.iterator(), values.iterator());
33+
return entries.iterator();
6734
}
6835

6936
@Override
7037
public int size() {
71-
return keys.size();
38+
return entries.size();
7239
}
7340

7441
}
7542

43+
transient Set<Entry<String, V>> entrySet;
44+
7645
/**
7746
* @return 键值对集合
7847
*/
@@ -90,16 +59,14 @@ public Set<Entry<String, V>> entrySet() {
9059
*/
9160
@Override
9261
public V put(String key, V value) {
93-
// 只存放非空值
94-
if (key != null && value != null) {
95-
keys.add(key);
96-
values.add(value);
62+
if (key != null) {
63+
entries.add(new SimpleEntry<>(key, value));
9764
}
9865
return null;
9966
}
10067

10168
/**
102-
* 获取 Key 对应的最后(新)的一个值
69+
* 获取与指定 key 匹配的最后(新)的一个值
10370
* @param key 键
10471
* @return 最后(新)的一个值
10572
*/
@@ -112,117 +79,176 @@ public V get(Object key) {
11279
}
11380

11481
/**
115-
* 获取 Key 对应的最后(新)的一个值
82+
* 获取与指定 key 匹配的最后(新)的一个值
11683
* @param key 键
11784
* @param ic 匹配 key 时是否忽略大小写
11885
* @return 匹配 key 的最后(新)的一个值
11986
*/
12087
public V get(String key, boolean ic) {
121-
for (int i = keys.size() - 1; i >= 0; i--) {
122-
String k = keys.get(i);
123-
if (ic) {
124-
if (k.equalsIgnoreCase(key)) {
125-
return values.get(i);
88+
if (key != null) {
89+
for (int i = entries.size() - 1; i >= 0; i--) {
90+
Entry<String, V> entry = entries.get(i);
91+
String k = entry.getKey();
92+
if (ic && key.equalsIgnoreCase(k) || !ic && key.equals(k)) {
93+
return entry.getValue();
12694
}
127-
} else if (k.equals(key)) {
128-
return values.get(i);
12995
}
13096
}
13197
return null;
13298
}
13399

134100
/**
135-
* 获取 Key 下的所有 Value
101+
* 获取与指定 key 匹配的所有值列表
136102
* @param key 键
137103
* @return List
138104
*/
139-
public List<V> getAll(String key) {
140-
return getAll(key, false);
105+
public List<V> list(String key) {
106+
return list(key, false);
141107
}
142108

143109
/**
144-
* 获取 Key 下的所有 Value
110+
* 获取与指定 key 匹配的所有值列表
145111
* @param key 键
146112
* @param ic 匹配 key 时是否忽略大小写
147113
* @return List
148114
*/
149-
public List<V> getAll(String key, boolean ic) {
115+
public List<V> list(String key, boolean ic) {
150116
List<V> list = new ArrayList<>();
151117
if (key != null) {
152-
for (int i = 0; i < keys.size(); i++) {
153-
String k = keys.get(i);
154-
if (ic) {
155-
if (k.equalsIgnoreCase(key)) {
156-
list.add(values.get(i));
157-
}
158-
} else if (k.equals(key)) {
159-
list.add(values.get(i));
118+
for (Entry<String, V> entry : entries) {
119+
String k = entry.getKey();
120+
if (ic && key.equalsIgnoreCase(k) || !ic && key.equals(k)) {
121+
list.add(entry.getValue());
160122
}
161123
}
162124
}
163125
return list;
164126
}
165127

128+
public boolean replace(String key, V oldValue, V newValue) {
129+
throw new UnsupportedOperationException();
130+
}
131+
166132
/**
167-
* 遍历,该方法是为兼容 Android 低版本
168-
* @param action The action to be performed for each entry
133+
* 替换与指定 key 匹配的最后(新)的一个值
134+
* @param key 键
135+
* @return 被替换的值
169136
*/
170-
public void forEach(BiConsumer<? super String, ? super V> action) {
171-
Platform.forEach(this, action);
137+
public V replace(String key, V value) {
138+
return replace(key, value, false);
172139
}
173140

174141
/**
175-
* 移除指定 键 和 值 的所有键值对
142+
* 替换与指定 key 匹配的最后(新)的一个值
176143
* @param key 键
177-
* @param value 值
178-
* @return true if the value was removed
144+
* @param ic 匹配 key 时是否忽略大小写
145+
* @return 被替换的值
179146
*/
180-
public boolean remove(Object key, Object value) {
181-
boolean removed = false;
182-
if (key instanceof String && value != null) {
183-
Iterator<Entry<String, V>> it = entrySet().iterator();
184-
while (it.hasNext()) {
185-
Entry<String, V> e = it.next();
186-
String k = e.getKey();
187-
V v = e.getValue();
188-
if (k.equals(key) && v.equals(value)) {
189-
it.remove();
190-
removed = true;
147+
public V replace(String key, V value, boolean ic) {
148+
if (key != null) {
149+
for (int i = entries.size() - 1; i >= 0; i--) {
150+
Entry<String, V> entry = entries.get(i);
151+
String k = entry.getKey();
152+
if (ic && key.equalsIgnoreCase(k) || !ic && key.equals(k)) {
153+
return entry.setValue(value);
191154
}
192155
}
193156
}
194-
return removed;
157+
return null;
158+
}
159+
160+
/**
161+
* 替换与指定 key 匹配的所有值
162+
* @param key 键
163+
* @return 被替换的键值对数量
164+
*/
165+
public int replaceAll(String key, V value) {
166+
return replaceAll(key, value, false);
195167
}
196168

197169
/**
198-
* 移除指定 指定键 的最后(新)一个值
170+
* 替换与指定 key 匹配的所有值
171+
* @param key 键
172+
* @param ic 匹配 key 时是否忽略大小写
173+
* @return 被替换的键值对数量
174+
*/
175+
public int replaceAll(String key, V value, boolean ic) {
176+
int count = 0;
177+
if (key != null) {
178+
for (Entry<String, V> entry : entries) {
179+
String k = entry.getKey();
180+
if (ic && key.equalsIgnoreCase(k) || !ic && key.equals(k)) {
181+
entry.setValue(value);
182+
}
183+
}
184+
}
185+
return count;
186+
}
187+
188+
/**
189+
* 遍历,该方法是为兼容 Android 低版本
190+
* @param action The action to be performed for each entry
191+
*/
192+
public void forEach(BiConsumer<? super String, ? super V> action) {
193+
Platform.forEach(this, action);
194+
}
195+
196+
public boolean remove(Object key, Object value) {
197+
throw new UnsupportedOperationException();
198+
}
199+
200+
/**
201+
* 移除与指定 key 匹配的最后(新)一个值
199202
* @param key 键
200203
* @return the value was removed
201204
*/
202205
public V remove(Object key) {
203206
if (key instanceof String) {
204-
for (int i = keys.size() - 1; i >= 0; i--) {
205-
if (keys.get(i).equals(key)) {
206-
keys.remove(i);
207-
return values.remove(i);
207+
return remove((String) key, false);
208+
}
209+
return null;
210+
}
211+
212+
/**
213+
* 移除与指定 key 匹配的最后(新)一个值
214+
* @param key 键
215+
* @param ic 匹配 key 时是否忽略大小写
216+
* @return the value was removed
217+
*/
218+
public V remove(String key, boolean ic) {
219+
if (key != null) {
220+
for (int i = entries.size() - 1; i >= 0; i--) {
221+
String k = entries.get(i).getKey();
222+
if (ic && key.equalsIgnoreCase(k) || !ic && key.equals(k)) {
223+
return entries.remove(i).getValue();
208224
}
209225
}
210226
}
211227
return null;
212228
}
213229

214230
/**
215-
* 移除指定 指定键 的 所有值
231+
* 移除与指定 key 匹配的所有值
216232
* @param key 键
217233
* @return the value was removed
218234
*/
219235
public List<V> removeAll(String key) {
236+
return removeAll(key, false);
237+
}
238+
239+
/**
240+
* 移除与指定 key 匹配的所有值
241+
* @param key 键
242+
* @param ic 匹配 key 时是否忽略大小写
243+
* @return the value was removed
244+
*/
245+
public List<V> removeAll(String key, boolean ic) {
220246
List<V> list = new ArrayList<>();
221247
if (key != null) {
222-
for (int i = keys.size() - 1; i >= 0; i--) {
223-
if (keys.get(i).equals(key)) {
224-
list.add(values.remove(i));
225-
keys.remove(i);
248+
for (int i = entries.size() - 1; i >= 0; i--) {
249+
String k = entries.get(i).getKey();
250+
if (ic && key.equalsIgnoreCase(k) || !ic && key.equals(k)) {
251+
list.add(entries.remove(i).getValue());
226252
}
227253
}
228254
}

okhttps/src/test/java/com/ejlchina/test/ListMapTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public void test2() {
4747
25, 30, 26
4848
};
4949

50-
List<Object> names = map.getAll("name");
50+
List<Object> names = map.list("name");
5151
Assert.assertEquals(3, names.size());
5252
Assert.assertEquals(NAMES[0], names.get(0));
5353
Assert.assertEquals(NAMES[1], names.get(1));
5454
Assert.assertEquals(NAMES[2], names.get(2));
5555

56-
List<Object> ages = map.getAll("age");
56+
List<Object> ages = map.list("age");
5757
Assert.assertEquals(3, ages.size());
5858
Assert.assertEquals(AGES[0], ages.get(0));
5959
Assert.assertEquals(AGES[1], ages.get(1));

0 commit comments

Comments
 (0)