Gravecard
Gravecard

Reputation: 116

How to speedup multiple searches on an HashTable

I have two files (with almost 5000 lines each) with logs. The files in each line has a set of rules associated too an email, like this:

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

I use the following function to read the file, and get the rules for each email:

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

So far so, good. In each "space" of the ArrayList I'll have an String[] containing the rules for each email. In other hand i have also an HashMap containing one unique list of rules and it's value like this:

RULE_NAME - VALUE
RULE_1 - 0.1
RULE_2 - 0.5
RULE_3 - 0.6
...

I need to compare every rule of every email too see if it exists on the HashMap. If exist returns the value of the rule for some calculations I use this function for that:

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

The problem is that i need to compare every email and it's rules multiple times (more then 10.000), since I'm using a Genetic Algorithm to try to optimize the VALUE of each RULE. Is there anyway to optimize the comparison of the rules of each email through the HASHMAP? Since i need speed, I'm doing 100 verifications in 8 minutes now.

Sorry for my english.

Regards

Upvotes: 0

Views: 107

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533660

The whole point of having a hash table is so youc an do a single hash lookup. If you are just going to loop through the keys, you may as well use a List.

I don't know where you are building your scores, but you can normalise the case.

scores.put(key.toLowerCase(), value);

for a case insensive lookup

Double d= scores.get(key.toLowerCase());

Upvotes: 2

Related Questions