如何检查某个元素是否存在于一组项目中?

发布于 2024-10-12 10:09:19 字数 224 浏览 1 评论 0原文

在 Java 的 if 语句中,如何检查某个对象是否存在于一组项目中。例如 在这种情况下,我需要验证水果是苹果、橙子还是香蕉。

if (fruitname in ["APPLE", "ORANGES", "GRAPES"]) {
    //Do something
}

这是一件非常微不足道的事情,但我无法找到一种简短的方法来完成此任务。

In an if statement in Java how can I check whether an object exists in a set of items. E.g.
In this scenario i need to validate that the fruit will be an apple, orange or banana.

if (fruitname in ["APPLE", "ORANGES", "GRAPES"]) {
    //Do something
}

It's a very trivial thing but I couldn't figure out a short and concise way to accomplish this.

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

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

发布评论

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

评论(4

后来的我们 2024-10-19 10:09:19
static final List<String> fruits = Arrays.asList("APPLE", "ORANGES", "GRAPES");

if (fruits.contains(fruitname))

如果您的列表更大,则一组会更有效。

static final Set<String> fruits = new HashSet<String>(
       Arrays.asList("APPLE", "ORANGES", "GRAPES", /*many more*/));
static final List<String> fruits = Arrays.asList("APPLE", "ORANGES", "GRAPES");

if (fruits.contains(fruitname))

If your list was much larger, a set would be more efficient.

static final Set<String> fruits = new HashSet<String>(
       Arrays.asList("APPLE", "ORANGES", "GRAPES", /*many more*/));
初懵 2024-10-19 10:09:19

为了完整性,使用 google-collections/guava:

import com.google.common.collect.Sets;

static final Set<String> fruit = Sets.newHashSet("APPLE", "ORANGES", "GRAPES");

if (fruit.contains(fruitname))

或使用plane旧的jdk类:

static final Set<String> fruit = new HashSet<String>(Arrays.asList("APPLE", "ORANGES", "GRAPES"));

for completeness using google-collections/guava:

import com.google.common.collect.Sets;

static final Set<String> fruit = Sets.newHashSet("APPLE", "ORANGES", "GRAPES");

if (fruit.contains(fruitname))

or using the plane old jdk classes:

static final Set<String> fruit = new HashSet<String>(Arrays.asList("APPLE", "ORANGES", "GRAPES"));
傲鸠 2024-10-19 10:09:19

Arrays.binarySearch 您在寻找什么?

String [] fruits = new String[]{"APPLE", "ORANGES", "GRAPES"};
Arrays.sort(fruits); // binarySearch requires that the array is sorted

if (Arrays.binarySearch(fruits), fruitname) >= 0) {
  // found!
}

当然还有值得信赖的Apache Commons ArrayUtils

if (ArrayUtils.contains(new String[]{"APPLE", "ORANGES", "GRAPES"}, fruitname){
  // found
}

我知道 Apache Commons 中会有一些东西:)

Is Arrays.binarySearch what you are looking for?

String [] fruits = new String[]{"APPLE", "ORANGES", "GRAPES"};
Arrays.sort(fruits); // binarySearch requires that the array is sorted

if (Arrays.binarySearch(fruits), fruitname) >= 0) {
  // found!
}

And of course the trusted Apache Commons ArrayUtils:

if (ArrayUtils.contains(new String[]{"APPLE", "ORANGES", "GRAPES"}, fruitname){
  // found
}

I knew there would be something in Apache Commons :)

九厘米的零° 2024-10-19 10:09:19

如果您有 SetListMap 的水果,它们都具有相同的父级:Collection,您可以尝试这个例子。

String fruitName = "Orange";
Collection<String> fruits = ... // set of fruits
if (fruits.contains(fruitName)) {
    ...
}

(有关创建文字 Set 的 Java 8/9/10 方法,请参阅这个 SO 答案。 )

请注意区分大小写(橙色!=橙色)。

If you have Set, List, Map of fruits which all have the same parent: Collection, you can try this example.

String fruitName = "Orange";
Collection<String> fruits = ... // set of fruits
if (fruits.contains(fruitName)) {
    ...
}

(For Java 8/9/10 ways of creating literal Set please see this SO answer.)

Be careful with case sensitivity (Orange != orange).

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