Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用值列表优化HashMap搜索_Java_List_Hashmap - Fatal编程技术网

Java 使用值列表优化HashMap搜索

Java 使用值列表优化HashMap搜索,java,list,hashmap,Java,List,Hashmap,我有一个映射,其中的值引用了对象列表 //key1.getElements() - produces the following [Element N330955311 ({}), Element N330955300 ({}), Element N3638066598 ({})] 我希望搜索每个键的列表,并找到给定元素(>=2)的出现点 目前我的方法非常慢,我有很多数据,我知道执行时间是相对的,但需要40秒~ 我的方法 public String occurance>=2 (Stri

我有一个映射,其中的值引用了对象列表

//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
之所以这么慢,是因为我有很多ID,我正在搜索-8000~并且我的地图中有3000~个密钥。因此其>8000*3000*8000(假设每个id/元素在键/值集映射中至少存在一次)

请帮助我以一种更有效的方式进行此搜索。我没有太深入地练习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;
}
public void findAdjacents(){
对于(int i=0;i
基于您提供的伪代码,无需迭代映射中的所有键。您可以直接在地图上获取(id)。如果映射有它,那么您将获得元素列表,您可以对其进行迭代,并在元素计数大于2时获得该元素。如果该id不存在,则将返回null。因此,在这种情况下,您可以对代码进行一些优化


谢谢

如果此代码工作正常(除了速度慢之外),您应该在我们的姐妹网站上提交。(为了警告你,他们需要的是正确的代码,而不是伪代码。)你想做什么还不是很清楚。请发布一些真实的代码。@shmosel,我已经添加了与问题相关的真实代码,谢谢。我已经通过后端重构对此进行了优化。