Java 中是否有相当于 Javascript 的“some”的方法?方法?

发布于 2024-12-28 17:39:39 字数 183 浏览 0 评论 0原文

我有一个集合,我想知道是否至少有一个元素满足某些条件。本质上, some 在 JavaScript 中所做的事情,我想做就收藏了!

I have a collection and I would like to know if at least one element meets some condition. Essentially, what some does in JavaScript, I would like to do on a collection!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若沐 2025-01-04 17:39:39

从 Java 8 开始,您可以将 Collection 转换为 Stream 并使用 anyMatch 如下例所示。

import java.util.Arrays;
import java.util.List;

public class SomeExample {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, -6, 7);
        boolean hasNegative = list.stream().anyMatch(x -> x < 0);
        if (hasNegative) {
            System.out.println("List contains some negative number");
        }
        else {
            System.out.println("List does not contain any negative number");
        }
    }
}

As of Java 8, you can convert the Collection into a Stream and use anyMatch as in the following example.

import java.util.Arrays;
import java.util.List;

public class SomeExample {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, -6, 7);
        boolean hasNegative = list.stream().anyMatch(x -> x < 0);
        if (hasNegative) {
            System.out.println("List contains some negative number");
        }
        else {
            System.out.println("List does not contain any negative number");
        }
    }
}
左耳近心 2025-01-04 17:39:39

查看 GuavaIterables 类及其 any() 实现。

或多或少与另一个答案中的 Commons Collections 示例相同,但通用化:

List<String> strings = Arrays.asList("ohai", "wat", "fuuuu", "kthxbai");
boolean well = Iterables.any(strings, new Predicate<String>() {
    @Override public boolean apply(@Nullable String s) {
        return s.equalsIgnoreCase("fuuuu");
    }
});
System.out.printf("Do any match? %s%n", well ? "Yep" : "Nope");

Check out Guava's Iterables class and its any() implementation.

More or less the same thing as the Commons Collections example in the other answer, but genericized:

List<String> strings = Arrays.asList("ohai", "wat", "fuuuu", "kthxbai");
boolean well = Iterables.any(strings, new Predicate<String>() {
    @Override public boolean apply(@Nullable String s) {
        return s.equalsIgnoreCase("fuuuu");
    }
});
System.out.printf("Do any match? %s%n", well ? "Yep" : "Nope");
星星的軌跡 2025-01-04 17:39:39

您可以使用 CollectionUtils 来自 Apache commons-collections

List<Integer> primes = Arrays.asList(3, 5, 7, 11, 13)
CollectionUtils.exists(primes, even);  //false

其中 even 是谓词:

Predicate even = new Predicate() {
    public boolean evaluate(Object object) {
        return ((Integer)object) % 2 == 0;
    }
}

或者在内联版本中:

List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13)
CollectionUtils.exists(primes, new Predicate() {
    public boolean evaluate(Object object) {
        return ((Integer)object) % 2 == 0;
    }
});

是的,它丑陋有两个原因:

  1. Java(尚)不支持作为一等公民的函数,这些函数是用 Single-Abstract-Method 接口模拟的。
  2. commons-collections 不支持泛型。

另一方面,在像 Scala 这样的现代 JVM 语言中,您可以编写:

List(3,5,7,11,13,17).exists(_ % 2 == 0)

You can use CollectionUtils from Apache commons-collections:

List<Integer> primes = Arrays.asList(3, 5, 7, 11, 13)
CollectionUtils.exists(primes, even);  //false

Where even is a predicate:

Predicate even = new Predicate() {
    public boolean evaluate(Object object) {
        return ((Integer)object) % 2 == 0;
    }
}

Or in an inlined version:

List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13)
CollectionUtils.exists(primes, new Predicate() {
    public boolean evaluate(Object object) {
        return ((Integer)object) % 2 == 0;
    }
});

Yes, it is ugly for two reasons:

  1. Java does not (yet) support functions as first-class citizens, which are emulated with Single-Abstract-Method interface.
  2. commons-collections does not support generics.

On the other hand in modern JVM languages like Scala you can write:

List(3,5,7,11,13,17).exists(_ % 2 == 0)
初雪 2025-01-04 17:39:39

Java 没有内置此功能。 Javascript 的 some() 接受函数指针作为参数,这不是 Java 本身支持的。但模拟 some( ) 在 Java 中使用循环和回调功能的接口。

Java doesn't have this feature built-in. Javascript's some() accepts a function pointer as an argument, which is not something that's natively supported in Java. But it should be fairly straight forward to emulate the functionality of some() in Java using a loop and and an interface for the callback functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文