我很快就写了这个片段来完成这项工作如何映射KVP列表地图<重点,列出<Value>> ..快
private void map() {
for (KVPair kvPair : content) {
String k = kvPair.getKey();
String v = kvPair.getValue();
if (mappedContent.containsKey(k)) {
List<String> values = mappedContent.get(k);
values.add(v);
} else {
List<String> values = new ArrayList<>();
values.add(v);
mappedContent.put(k, values);
}
}
}
它的工作原理和使用时跑了1K,2K,4K和8K的随机数据,我得到以下性能(平均10万个奔跑)
Running with 1,000 pairs
[perfRun] 100000 iterations took 3 seconds
[perfRun] Run time: 3758786000 ns. 1 iteration takes 37 us
Running with 2,000 pairs
[perfRun] 100000 iterations took 6 seconds
[perfRun] Run time: 6675544000 ns. 1 iteration takes 66 us
Running with 4,000 pairs
[perfRun] 100000 iterations took 13 seconds
[perfRun] Run time: 13337145000 ns. 1 iteration takes 133 us
Running with 8,000 pairs
[perfRun] 100000 iterations took 27 seconds
[perfRun] Run time: 27109480000 ns. 1 iteration takes 271 us
粗略地讲,当大小双打,双打的时间。我会采取线性增长的方式,但我们仍然想,我们可以做得更好吗?用恒定的时间映射东西是否可能?
也许如果你告诉我们你在做什么。 – 2013-03-08 18:34:07
您是否尝试使用CommonValueMultiValueMap而不是滚动自己的? – Alb 2013-03-08 18:34:33