lundi 16 décembre 2019

Java 8 Streams: How to stream and optimize nested fors and an if statement [closed]

I am learning about streams and trying to convert some old code into Java 8 Streams.

This is the use case I am trying convert:

  • exmaple.getAttributeByName("nestedObject")) is a generic method that returns any object type. In my use case its a list of strings.
   private static void nestedForAndIfMethod(Set<Exmaple> examples, List<Errors> invalidExmaples) {
        for (final Exmaple exmaple : examples) {
            for (String element: (String[]) exmaple.getAttributeByName("nestedObject")) {
                if (elementIsinvalid(element)) {
                    final String errorCause = String.format("Error from the element '%s' in the example '%s'", element, example);
                    updateErrorLog(invalidExmaples, example, errorCause);
                }
            }
        }
    }

I've tried a few things but they don't work at all and just return errors.

Aucun commentaire:

Enregistrer un commentaire