有一个Java等价于Javascript的“一些”方法? [英] is there a Java equivalent to Javascript's "some" method?
本文介绍了有一个Java等价于Javascript的“一些”方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个集合,我想知道至少一个元素是否满足一些条件。基本上,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!
推荐答案
查看 Guava 的 Iterables
类及其any()$c $ c>
实现。
Check out Guava's Iterables
class and its any()
implementation.
与其他答案中的Commons Collections示例相同,但是泛化:
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");
这篇关于有一个Java等价于Javascript的“一些”方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文