[英]comparing Hashmaps by different String Keys
i have two HashMaps and want compare it as fast as possible but the problem is, the String of mapA consist of two words connected with a space. 我有两个HashMaps并希望尽可能快地比较它,但问题是,mapA的字符串由两个连接空格的单词组成。 The String of mapB is only one word.
mapB的字符串只有一个字。
I dont want to count the occurences, that is already done, i want to compare the two diferent Strings 我不想计算已经完成的事件,我想比较两个不同的字符串
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
the first key of mapA should find key "hello" and key "world" from mapB mapA的第一个键应该从mapB中找到键“hello”和键“world”
what i trying to do is parsing a long Text to find Co occurences and set a value how often they occur related to all words. 我想要做的是解析一个长文本来找到Co出现并设置一个值,它们发生的频率与所有单词有关。
my first try: 我的第一次尝试:
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);**
}
}
}
You shouldn't iterate over a HashMap
(O(n)) if you are just trying to find a particular key, that's what the HashMap
lookup (O(1)) is used for. 如果您只是想要查找特定键,则不应迭代
HashMap
(O(n)),这就是HashMap
查找(O(1))的用途。 So eliminate your inner loop. 所以消除你的内循环。
Also you can eliminate a few unnecessary variables in your code (eg key
, tokenkey
). 您还可以在代码中消除一些不必要的变量(例如
key
, tokenkey
)。 You also don't need a third tokens
map, you can put the token values in mapb
. 您也不需要第三个
tokens
地图,您可以将令牌值放在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);**
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.