Trying to find a better way of finding something rather then looping through list from top to bottom. 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.

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())
                &amp;&amp; match(contextRule.getMessageContext().getApplication(), messageContext.getApplication())
                &amp;&amp; match(contextRule.getMessageContext().getService(), messageContext.getService())
                &amp;&amp; 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(String application);      
  public String getService();      
  public void setService(String service);      
  public String getOperation();      
  public void setOperation(String operation);
}


And finally ContextRule

public interface ContextRule {
  public Context getMessageContext();  
  public int getAllowedConcurrentRequests();  
}


any help or suggestions appreciated.
Can your context rules be precomputed into any sort of hierarchical match tree once at application initialization?

HashMap&lt;String, HashMap&lt;String, List&lt;ContextRule&gt;&gt;&gt; contextRuleMap

public boolean match (Context context, Map&lt;String, Map&lt;String, List&lt;ContextRule&gt;&gt; contextRuleMap)
{
    Map&lt;String, List&lt;ContextRule&gt;&gt; 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.