我有两个文件(每行近5000行)和日志。每行中的文件都有一组与电子邮件关联的规则,如下所示:
Y#12#EMAIL_1#RULE_1,RULE_2,RULE_3,RULE_4#time=993470174
Y#12#EMAIL_2#RULE_1,RULE_2,RULE_3,RULE_4#time=993470175
Y#12#EMAIL_3#RULE_1,RULE_2,RULE_3#time=9934701778
我使用以下函数来读取文件,并获取每封电子邮件的规则:
private void processFile()
{
ArrayList<String[]> lSplitRules = new ArrayList<>();
try {
FileInputStream fileStream = new FileInputStream("log.log");
DataInputStream fileIn = new DataInputStream(fileStream);
BufferedReader fileBr = new BufferedReader(new InputStreamReader(fileIn));
String strLine;
while ((strLine = fileBr.readLine()) != null)
{
String[] lTokens = strLineSpam.split("#");
String lRawRules = lTokens[3];
lSplitRules.add(lRawRules.split(","));
}
} catch (FileNotFoundException e) {
System.out.println("File: log.log, not found. Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("Couldn't open log.log. Error: " + e.getMessage());
}
到目前为止,很好。在ArrayList的每个“空格”中,我将包含一个String [],其中包含每封电子邮件的规则。另一方面,我还有一个包含一个唯一规则列表的HashMap,它的值如下:
RULE_NAME - VALUE
RULE_1 - 0.1
RULE_2 - 0.5
RULE_3 - 0.6
...
我需要比较每封电子邮件的每条规则,看看它是否存在于HashMap中。如果存在则返回某些计算的规则值 我使用这个函数:
private Double eval (String rule, Map<String, Double> scores)
{
for (Entry<String, Double> entry : scores.entrySet()) {
if (entry.getKey().equalsIgnoreCase(rule))
{
return entry.getValue();
}
}
return 0.0;
}
问题在于我需要多次比较每个电子邮件及其规则(超过10.000),因为我正在使用遗传算法来尝试优化每个规则的值。无论如何通过HASHMAP优化每封电子邮件的规则比较?由于我需要速度,我现在在8分钟内做100次验证。
抱歉我的英文。
此致
答案 0 :(得分:2)
拥有哈希表的重点是让你做一个哈希查找。如果你只是要遍历键,你也可以使用List。
我不知道您在构建scores
的位置,但可以将案例规范化。
scores.put(key.toLowerCase(), value);
用于案例密集查找
Double d= scores.get(key.toLowerCase());