Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (https://msdn.microsoft.com/en-us/library/vstudio/bb534972%28v=vs.100%29.aspx).

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?

like image 357
Mirodinho Avatar asked Jun 19 '15 14:06

Mirodinho


People also ask

Is Java similar to C?

C is a middle-level language as it binds the bridges between machine-level and high-level languages. Java is a high-level language as the translation of Java code takes place into machine language, using a compiler or interpreter. C is only compiled and not interpreted. Java is both compiled and interpreted.

Is Java built on C?

The very first Java compiler was developed by Sun Microsystems and was written in C using some libraries from C++. Today, the Java compiler is written in Java, while the JRE is written in C.

Is Java similar to C or C++?

These two languages are very similar in terms of syntax and language features. They are so similar that if you're shown some portion of C++ code from a project and asked whether it's C++ or Java code, you may confuse yourself.

Is C# equivalent to Java?

Java and C# are incredibly similar. Both languages are somewhat derived from C++ and from similar first principles. Java was developed in 1995 to create a language with a simpler programming model than C++ while still preserving some of the same syntax of the language to facilitate developers transitioning to it.


1 Answers

With Java 8 you can write something like:

if (Stream.of(">", "<", "&", "l", "p").anyMatch(string::contains)) {   ... } 

Out of curiosity I ran a benchmark to compare this method vs a regex. Code and results below (lower score = faster). Streams perform an order of magnitude better than regex.

Benchmark                                    (s)  Mode  Samples     Score    Error  Units c.a.p.SO30940682.stream   >aaaaaaaaaaaaaaaaaaaaa  avgt       10    49.942 ±  1.936  ns/op c.a.p.SO30940682.stream   aaaaaaaaaaaaaaaaaaaaa>  avgt       10    54.263 ±  1.927  ns/op c.a.p.SO30940682.stream   aaaaaaaaaaaaaaaaaaaaap  avgt       10   131.537 ±  4.908  ns/op c.a.p.SO30940682.stream   paaaaaaaaaaaaaaaaaaaaa  avgt       10   129.528 ±  7.352  ns/op c.a.p.SO30940682.regex    >aaaaaaaaaaaaaaaaaaaaa  avgt       10   649.867 ± 27.142  ns/op c.a.p.SO30940682.regex    aaaaaaaaaaaaaaaaaaaaa>  avgt       10  1047.122 ± 89.230  ns/op c.a.p.SO30940682.regex    aaaaaaaaaaaaaaaaaaaaap  avgt       10  1029.710 ± 61.055  ns/op c.a.p.SO30940682.regex    paaaaaaaaaaaaaaaaaaaaa  avgt       10   694.309 ± 32.675  ns/op 

Code:

@State(Scope.Benchmark) @BenchmarkMode(Mode.AverageTime) public class SO30940682 {    @Param({">aaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaa>",           "aaaaaaaaaaaaaaaaaaaaap", "paaaaaaaaaaaaaaaaaaaaa"}) String s;    @Benchmark public boolean stream() {     return Stream.of(">", "<", "&", "l", "p").anyMatch(s::contains);   }    @Benchmark public boolean regex() {     return s.matches("^.*?(>|<|&|l|p).*$");   } } 
like image 192
assylias Avatar answered Sep 20 '22 21:09

assylias