in

Can I condense this for loop to a Collection using functional programming?


I have some code in my ANTLR project that looks like this:

ArrayList<MSSyntaxTree> procArgs = new ArrayList<>();
if (ctx.args() != null) {
    for (ParseTree pt : ctx.args().expr()) {
        procArgs.add(this.map.get(pt));
    }
}

ArrayList<MSSyntaxTree> lambdaArgs = new ArrayList<>();
if (ctx.lambdaArgs() != null) {
    for (ParseTree pt : ctx.lambdaArgs().expr()) {
        lambdaArgs.add(this.map.get(pt));
    }
}

While this works, I’d like to not have to do a null check, iterate over the ParseTree objects, then add them to a list. Is there a way to do this all in one line with Java’s Collections API or its functional programming paradigms?



Source: https://stackoverflow.com/questions/70628778/can-i-condense-this-for-loop-to-a-collection-using-functional-programming

.strip not working on certain strings [python]

Create forms using Redux And React