patternjavaMinor
Trying to find a better way of finding something rather then looping through list from top to bottom.
Viewed 0 times
loopingfindtopthroughtryingwaybetterratherbottomthen
Problem
I am having a slight issue here, i am trying to think of a faster way of finding two elements from 2 different lists that match. The problem is that if i have lists which both have 1000 elements ( rules ) and the very last one [index.1000] matches then in order to find it i have to loop through booth lists from top to bottom. What i have is fine and it works however its not very efficient. Could any one perhaps suggest a better way ? (if there is any ? )
The loop in this meethod is where the iteration through the lists happen. For furthere reference below this method you will find what ContextRule and Context is.
match() method which does comparason.
Context ( which is an interface )
```
public interface Context {
public String getUser();
public void setUser(String user);
public String getApplication();
public void setApplication(Stri
The loop in this meethod is where the iteration through the lists happen. For furthere reference below this method you will find what ContextRule and Context is.
public ContextRule match(Context messageContext, ContextRuleList contextRules) {
ContextRule matchedContextRule = null;
for(ContextRule contextRule : contextRules) {
if(this.match(messageContext, contextRule)) {
matchedContextRule = contextRule;
break;
}
}
if(matchedContextRule == null) {
matchedContextRule = this.getDefaultContextRule();
}
return matchedContextRule;
}match() method which does comparason.
private boolean match(Context messageContext, ContextRule contextRule) {
return match(contextRule.getMessageContext().getUser(), messageContext.getUser())
&& match(contextRule.getMessageContext().getApplication(), messageContext.getApplication())
&& match(contextRule.getMessageContext().getService(), messageContext.getService())
&& match(contextRule.getMessageContext().getOperation(), messageContext.getOperation());
}
private boolean match(String value, String contextValue) {
return value.equals(ContextRuleEvaluator.WILDCARD) || value.equals(contextValue);
}Context ( which is an interface )
```
public interface Context {
public String getUser();
public void setUser(String user);
public String getApplication();
public void setApplication(Stri
Solution
Can your context rules be precomputed into any sort of hierarchical match tree once at application initialization?
You can pre-compute your 'match tree' to as many 'levels deep' as you need to by User -> Application -> Service -> Operation to achieve the necessary performance.
HashMap>> contextRuleMap
public boolean match (Context context, Map> contextRuleMap)
{
Map> rulesByApplication = contextRuleMap[context.getUser()];
for (ContextRule contextRule : rulesByApplication[context.getApplication()]) {
// Existing logic for remaining 'match' of service and operation
}
return false;
}You can pre-compute your 'match tree' to as many 'levels deep' as you need to by User -> Application -> Service -> Operation to achieve the necessary performance.
Code Snippets
HashMap<String, HashMap<String, List<ContextRule>>> contextRuleMap
public boolean match (Context context, Map<String, Map<String, List<ContextRule>> contextRuleMap)
{
Map<String, List<ContextRule>> rulesByApplication = contextRuleMap[context.getUser()];
for (ContextRule contextRule : rulesByApplication[context.getApplication()]) {
// Existing logic for remaining 'match' of service and operation
}
return false;
}Context
StackExchange Code Review Q#35357, answer score: 2
Revisions (0)
No revisions yet.