簡體   English   中英

java程序如何檢查數組是否包含給定的兩個不同值?

[英]How to java Program to Check if an array contains a given two different values?

我想編寫一個代碼來檢查列表中給定兩個值的存在。

List<Tag> tags = new ArrayList<>();

要求是僅當 List tag包含"start""end"值時才返回true 我的代碼是這樣的,但是不符合要求。

public static boolean checkStartAndEndTimeTag(List<Tag> tags) {
        boolean isSuccess = false;
        int count = 0;
        for (Tag tag : tags) {
            if (tag.getKey().equals("start") || tag.getKey().equals("end")) {
                count++;
                if (count == 2)
                    break;
                isSuccess = true;
            }

        }
        return isSuccess;

有人可以幫我解決這個問題嗎?

這個...

if (count == 2)
  break;
isSuccess = true;

沒有意義。 即使只有一場比賽,這也會設置isSuccess

冗長的方法

好的,讓我們假設您只關心是否至少有一個start和一個end (排除重復項)。 一種方法是使用狀態標志,一種用於start ,一種用於end 為簡單起見,它們將從0開始,但最多只能為1 (因為我們不想要重復項),然后您可以執行類似...

public static boolean checkStartAndEndTimeTag(List<Tag> tags) {
    boolean isSuccess = false;
    int starts = 0;
    int ends = 0;
    for (Tag tag : tags) {
        if (tag.getKey().equals("start")) {
            starts = 1;
        } else if (tag.getKey().equals("end")) {
            ends = 1;
        }
    }
    isSuccess = (starts + ends) == 2;
    return isSuccess;
}

好的,你不需要isSuccess = (starts + ends) == 2; 並且可以簡單地return比較的結果。 你也可以跳出循環 if (starts + ends) == 2並避免不必要的計算

for (Tag tag : tags) {
    if (tag.getKey().equals("start")) {
        starts = 1;
    } else if (tag.getKey().equals("end")) {
        ends = 1;
    }

    if ((starts + ends) == 2) {
        break;
    }
}

使用流...

一種方法可能是利用流支持並簡單地filter Listcount結果,例如......

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        List<Tag> tags = new ArrayList<Tag>(25);
        tags.add(new Tag("begin"));
        tags.add(new Tag("front"));
        tags.add(new Tag("start"));
        tags.add(new Tag("finish"));
        tags.add(new Tag("tail"));
        tags.add(new Tag("end"));

        boolean isSuccessful = tags.stream().filter(tag -> tag.getKey().equals("start") || tag.getKey().equals("end")).count() >= 2;
        System.out.println(isSuccessful);
    }

    public class Tag {
        private String key;

        public Tag(String key) {
            this.key = key;
        }

        public String getKey() {
            return key;
        }


    }
}

更新...

好吧,這很快就變得復雜了。 假設您不想匹配兩個start標簽,因此您必須同時擁有一個end標簽和一個start標簽

因此,使用上面的示例,我們可以修改Tag類以支持equals (以及擴展hashcode

public class Tag {

    private String key;

    public Tag(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    @Override
    public String toString() {
        return getKey();
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 73 * hash + Objects.hashCode(this.key);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Tag other = (Tag) obj;
        if (!Objects.equals(this.key, other.key)) {
            return false;
        }
        return true;
    }

}

然后我們可以簡單地使用distinct過濾掉任何重復項,例如......

boolean isSuccessful = tags
        .stream()
        .distinct()
        .filter(tag -> tag.getKey().equals("start") || tag.getKey().equals("end"))
        .count() >= 2;

可能不是最有效的解決方案,但肯定是最短的解決方案之一

在這段代碼中

if (tag.getKey().equals("start") || tag.getKey().equals("end")) {
     count++;
     if (count == 2)
            break;
     isSuccess = true;
}

每當tagstartend時,您都將isSuccess設置為 true 。

更好的方法是

if (tag.getKey().equals("start") || tag.getKey().equals("end")) {
    count++;
    if (count == 2)
        return true;
}

你也可以使用

tags.stream()
    .map(Tag::getKey)
    .distinct()
    .filter(t -> "start".equals(t) || "end".equals(t))
    .count() == 2;

如果列表包含 statt 或 end 兩次,這也將解決您的原始代碼錯誤地返回 true 的問題。

關於什么

tags.containsAll(Arrays.asList(new Tag("start"), new Tag("stop")))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM