mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
enhance CollUtil.zip
This commit is contained in:
parent
2f44d68e0b
commit
ac7a6edb25
@ -6,6 +6,8 @@
|
||||
## 4.6.3
|
||||
|
||||
### 新特性
|
||||
* 【core】 改进CollUtil.zip逻辑,减少内存复制(issue#I10T01@Gitee)
|
||||
|
||||
### Bug修复
|
||||
* 【http】 修复HttpRquest中body方法长度计算问题(issue#I10UPG@Gitee)
|
||||
|
||||
|
@ -1517,13 +1517,14 @@ public class CollUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<K> keyList = new ArrayList<K>(keys);
|
||||
final List<V> valueList = new ArrayList<V>(values);
|
||||
int entryCount = Math.min(keys.size(), values.size());
|
||||
final Map<K, V> map = newHashMap(entryCount);
|
||||
|
||||
final int size = Math.min(keys.size(), values.size());
|
||||
final Map<K, V> map = new HashMap<K, V>((int) (size / 0.75));
|
||||
for (int i = 0; i < size; i++) {
|
||||
map.put(keyList.get(i), valueList.get(i));
|
||||
final Iterator<K> keyIterator = keys.iterator();
|
||||
final Iterator<V> valueIterator = values.iterator();
|
||||
while (entryCount > 0) {
|
||||
map.put(keyIterator.next(), valueIterator.next());
|
||||
entryCount--;
|
||||
}
|
||||
|
||||
return map;
|
||||
|
@ -571,4 +571,19 @@ public class CollUtilTest {
|
||||
String last = CollUtil.getLast(test);
|
||||
Assert.assertNull(last);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipTest() {
|
||||
Collection<String> keys = CollUtil.newArrayList("a", "b", "c", "d");
|
||||
Collection<Integer> values = CollUtil.newArrayList(1, 2, 3, 4);
|
||||
|
||||
Map<String, Integer> map = CollUtil.zip(keys, values);
|
||||
|
||||
Assert.assertEquals(4, map.size());
|
||||
|
||||
Assert.assertEquals(1, map.get("a").intValue());
|
||||
Assert.assertEquals(2, map.get("b").intValue());
|
||||
Assert.assertEquals(3, map.get("c").intValue());
|
||||
Assert.assertEquals(4, map.get("d").intValue());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user