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