简体   繁体   English

使用值列表搜索HashMap的优化

[英]Optimisation of searching HashMap with list of values

I have a map in which values have references to lists of objects. 我有一张地图,其中的值引用了对象列表。

//key1.getElements()  - produces the following
[Element N330955311 ({}), Element N330955300 ({}), Element N3638066598 ({})]

I would like to search the list of every key and find the occurrence of a given element (>= 2). 我想搜索每个键的列表,并找到给定元素(> = 2)的出现。

Currently my approach to this is every slow, I have a lot of data and I know execution time is relative but it takes 40seconds~. 目前,我的处理方法很慢,我有很多数据,而且我知道执行时间是相对的,但要花40秒〜。

My approach.. 我的方法

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

The reason why this is so slow is because I have a lot of ids which I'm searching for - 8000~ and I have 3000~ keys in my map. 之所以这么慢,是因为我要搜索的ID太多-8000〜并且我的地图中有3000〜键。 So its > 8000*3000*8000 (given that every id/element exists in the key/valueSet map at least once) 因此其> 8000 * 3000 * 8000(假设每个id /元素在键/ valueSet映射中至少存在一次)

Please help me with a more efficient way to make this search. 请以一种更有效的方式帮助我进行搜索。 I'm not too deep into practicing Java, so perhaps there's something obvious I'm missing. 我不太想练习Java,所以也许我缺少一些明显的东西。

Edited in real code after request: 请求后以真实代码进行编辑:

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;
}

Based on the pseudocode provided by you, there is no need to iterate all the keys in the Map. 根据您提供的伪代码,无需迭代Map中的所有键。 You can directly do a get(id) on the map. 您可以直接在地图上执行get(id)。 If the Map has it, you will get the list of elements on which you can iterate and get the element if its count is > 2. If the id is not there then null will be returned. 如果Map包含该元素,则将获得可以迭代的元素列表,如果count大于2,则获得该元素。如果id不存在,则将返回null。 So in that case you can optimize your code a bit. 因此,在这种情况下,您可以稍微优化一下代码。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM