Java 相当于 C# 的 'Enumerable.Any'

2022-09-01 15:32:18

在 C# 中,有一种方法可以减少 if 语句的长度,方法是检查序列中的元素是否满足条件(https://msdn.microsoft.com/en-us/library/vstudio/bb534972%28v=vs.100%29.aspx)。Enumerable.Any

例如,代替:

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

我们可以使用

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

有没有一种等效的,如果不是更好的,在Java中做到这一点的方法?


答案 1

使用Java 8,你可以写这样的东西:

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

出于好奇,我运行了一个基准测试来比较此方法与正则表达式。下面的代码和结果(分数越低 = 越快)。流的性能比正则表达式好一个数量级。

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

法典:

@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).*$");
  }
}

答案 2

在 Java 8 中,使用 anyMatch 方法可以做到这一点:

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

}

推荐