Get certification rules from collection How could the following function (the for loop) be transformed to a lambda expression or refactored to be cleaner:

public static Collection&lt;AccessRuleTemplate&gt; getCaRules(String roleName, Collection&lt;Integer&gt; currentcas) {
    Collection&lt;AccessRuleTemplate&gt; result = new ArrayList&lt;AccessRuleTemplate&gt;();
    boolean allcafound = false;
    List&lt;AccessRuleTemplate&gt; carules = new ArrayList&lt;AccessRuleTemplate&gt;();
    for (Integer caId : currentcas) {
        if (caId.equals(Integer.valueOf(BasicAccessRuleSet.CA_ALL))) {
            allcafound = true;
            break;
        }
        carules.add(new AccessRuleTemplate(StandardRules.CAACCESS.resource() + caId.toString(), AccessRuleState.RULE_ACCEPT, false));
    }
    if (allcafound) {
        carules.clear();
        carules.add(new AccessRuleTemplate(StandardRules.CAACCESSBASE.resource(), AccessRuleState.RULE_ACCEPT, true));
    }
    result.addAll(carules);
    return result;

}


In particular i don't like the use of boolean flag and would like to avoid it. The idea with the function is to traverse list of ids and if a particular id found it breaks the loop and sets a flag to true and then checks the flag later and clears idlist and only adds something to list. Otherwise all other ids are added to list.
There's a lot of waste in this method.  You're copying things to idList, only to clear them.  You're creating idList, only to copy it to result.

Try this:

public static Collection&lt;AccessRuleTemplate&gt; getCaRules(String roleName, Collection&lt;Integer&gt; currentcas) {
    List&lt;AccessRuleTemplate&gt; carules = new ArrayList&lt;&gt;();
    if (currentcas.contains(Integer.valueOf(BasicAccessRuleSet.CA_ALL))) {
        carules.add(new AccessRuleTemplate(StandardRules.CAACCESSBASE.resource(), AccessRuleState.RULE_ACCEPT, true));
    } else {
        for (Integer caId : currentcas) {
            carules.add(new AccessRuleTemplate(StandardRules.CAACCESS.resource() + caId.toString(), AccessRuleState.RULE_ACCEPT, false));
        }
    }
    return carules;
}


So, in the case where the item is found, there's a big savings - you don't create a bunch of objects and discard them.
In the case where the item is not found, there's a very slight increase in time to traverse the list twice.
In both cases, the copying between lists is eliminated, making this much more efficient (and readable).

I'd also recommend that for the "found" case, you pre-create a static, immutable, singleton list which you can directly return each time.

private static final Collection&lt;AccessRuleTemplate&gt; BASE_ACCESS_TEMPLATE
        = Collections.singletonList(new AccessRuleTemplate(StandardRules.CAACCESSBASE.resource(), AccessRuleState.RULE_ACCEPT, true));


Also "roleName" is unused.  I'm guessing it was only used in logging which was stripped out.

Finally, though you could use a lambda function for the conversion loop, I think the code above is simpler.  If you really want to, it would look something like:

return currentcas.stream()
        .map(caId -&gt; {new AccessRuleTemplate(StandardRules.CAACCESS.resource() + caId.toString(), AccessRuleState.RULE_ACCEPT, false)})
        .collect(Collectors.toList());