Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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 通过不同的字符串键比较哈希映射_Java_Hashmap_Information Retrieval - Fatal编程技术网

Java 通过不同的字符串键比较哈希映射

Java 通过不同的字符串键比较哈希映射,java,hashmap,information-retrieval,Java,Hashmap,Information Retrieval,我有两个HashMap,希望尽快对其进行比较,但问题是,mapA的字符串由两个用空格连接的单词组成。mapB的字符串只有一个单词 我不想计算发生次数,这已经完成,我想比较两个不同的字符串 mapA: key: hello world, value: 10 key: earth hi, value: 20 mapB: key: hello, value: 5 key: world, value: 15 key: earth, value: 25 key: hi, value:

我有两个HashMap,希望尽快对其进行比较,但问题是,mapA的字符串由两个用空格连接的单词组成。mapB的字符串只有一个单词


我不想计算发生次数,这已经完成,我想比较两个不同的字符串

mapA: 
key: hello world, value: 10 
key: earth hi, value: 20

mapB:  
key: hello, value: 5  
key: world, value: 15
key: earth, value: 25
key: hi,    value: 35
mapA的第一个键应该从mapB中找到键“hello”和键“world”

我试图做的是解析一个长文本以查找共现,并设置一个值,即它们与所有单词相关的出现频率

我的第一次尝试:

for(String entry : mapA.keySet())
    {
String key = (String) entry;
      Integer mapAvalue = (Integer) mapA.get(entry);
      Integer tokenVal1=0, tokenVal2=0;
      String  token1=key.substring(0, key.indexOf(" "));
      String      token2=key.substring(key.indexOf(" "),key.length()).trim();
         for( String mapBentry : mapb.keySet())
        {
            String tokenkey = mapBentry;
            if(tokenkey.equals(token1)){
                tokenVal1=(Integer)tokens.get(tokenentry);
            }
            if(tokenkey.equals(token2)){
                tokenVal2=(Integer)tokens.get(tokenentry);
            }
            if(token1!=null && token2!=null && tokenVal1>1000 && tokenVal2>1000 ){

                **procedurecall(mapAvalue, token1, token2, tokenVal1, tokenVal2);**


             }
        }


    }

如果您只是试图找到一个特定的键,那么不应该迭代
HashMap
(O(n)),这就是
HashMap
查找(O(1))的用途。所以,消除你的内部循环

您还可以消除代码中一些不必要的变量(例如,
key
tokenkey
)。您也不需要第三个
令牌
映射,您可以将令牌值放入
mapb

for(String entry : mapA.keySet())
{
  Integer mapAvalue = (Integer) mapA.get(entry);
  String  token1=entry.substring(0, entry.indexOf(" "));
  String  token2=entry.substring(entry.indexOf(" "),entry.length()).trim();

  if(mapb.containsKey(token1) && mapb.containskey(token2))
  {
       // look up the tokens:
       Integer tokenVal1=(Integer)mapb.get(token1);
       Integer tokenVal2=(Integer)mapb.get(token2);

       if(tokenVal1>1000 && tokenVal2>1000)
       {
            **procedurecall(mapAvalue, token1, token2, tokenVal1, tokenVal2);**
       }
  }

如果您只是试图找到一个特定的键,那么不应该迭代
HashMap
(O(n)),这就是
HashMap
查找(O(1))的用途。所以,消除你的内部循环

您还可以消除代码中一些不必要的变量(例如,
key
tokenkey
)。您也不需要第三个
令牌
映射,您可以将令牌值放入
mapb

for(String entry : mapA.keySet())
{
  Integer mapAvalue = (Integer) mapA.get(entry);
  String  token1=entry.substring(0, entry.indexOf(" "));
  String  token2=entry.substring(entry.indexOf(" "),entry.length()).trim();

  if(mapb.containsKey(token1) && mapb.containskey(token2))
  {
       // look up the tokens:
       Integer tokenVal1=(Integer)mapb.get(token1);
       Integer tokenVal2=(Integer)mapb.get(token2);

       if(tokenVal1>1000 && tokenVal2>1000)
       {
            **procedurecall(mapAvalue, token1, token2, tokenVal1, tokenVal2);**
       }
  }

再次定义“比较”。您的问题应该提供您试图实现的方法的签名和返回类型以及javadoc。你的描述太模糊了。但问题到底是什么?“我想比较两个不同的字符串”如何?再看一次示例,定义“比较”。您的问题应该提供您试图实现的方法的签名和返回类型以及javadoc。你的描述太模糊了。但问题到底是什么?“我想比较两个不同的字符串”如何?看看这个例子