vendredi 19 juin 2015

Java equivalent of C#'s 'Enumerable.Any'

In C# there is a way of reducing the length of an if-statement by using Enumerable.Any to check if elements in a sequence satisfy a condition (http://ift.tt/1GVXgdl).

For example Instead of:

If ( string.Contains(">") || string.Contains("<") || string.Contains("&") || string.Contains("l") || string.Contains("p") )

We can use

if (new [] { ">", "<", "&", "l", "p"}.Any(w => string.Contains(w)))

Is there an equivalent, if not better, way of doing this in Java?

Aucun commentaire:

Enregistrer un commentaire