0
我有一个地图,其中值包含对象列表的引用。使用值列表搜索HashMap的优化
//key1.getElements() - produces the following
[Element N330955311 ({}), Element N330955300 ({}), Element N3638066598 ({})]
我想搜索每个键的列表并找到给定元素(> = 2)的出现。
目前我的方法是每一个缓慢的,我有很多的数据,我知道执行时间是相对的,但它需要40秒〜。
我的办法..
public String occurance>=2 (String id)
//Search for id
//Outer loop through Map
//get first map value and return elements
//inner loop iterating through key.getElements()
//if match with id..then iterate count
//return Strings with count == 2 else return null
之所以这样,是如此之慢是因为我有很多的IDS其中我在寻找的 - 8000〜和我在我的地图3000〜键。所以它> 8000 * 3000 * 8000(假设每个id /元素至少存在一次key/valueSet映射)
请帮助我一个更有效的方法来进行此搜索。我并没有太深入练习Java,所以也许有一些明显的我错过了。
编辑在真正的代码请求后:
public void findAdjacents() {
for (int i = 0; i < nodeList.size(); i++) {
count = 0;
inter = null;
container = findIntersections(nodeList.get(i));
if (container != null) {
intersections.add(container);
}
}
}
public String findIntersections(String id) {
Set<Map.Entry<String, Element>> entrySet = wayList.entrySet();
for (Map.Entry entry : entrySet) {
w1 = (Way) wayList.get(entry.getKey());
for (Node n : w1.getNodes()) {
container2 = String.valueOf(n);
if (container2.contains(id)) {
count++;
}
if (count == 2) {
inter = id;
count = 0;
}
}
}
if (inter != (null))
return inter;
else
return null;
}
如果这个代码工作正常(除了一个事实,即它是缓慢的),你应该提交我们的[代码审查(https://codereview.stackexchange.com/)的姐妹网站。 (并警告你,他们会想要正确的代码,而不是伪代码。) –
你试图做什么并不是很清楚。请张贴一些真实的代码。 – shmosel
@shmosel,我已经添加了与问题有关的真实代码,谢谢 – AnthonyJ