Java HashSet<> :如果HashSet包含指定值以外的值,则返回false

时间:2017-11-22 04:20:54

标签: java hashset

所以我提取了一个字符串列表,例如{“ADD”,“DEL”,“CHG”,“DEL”,“NA”,“ADD”,“BLAH”,“YAK”,....} JSON请求并将它们传递到哈希集以避免重复。 如果HashSet除了“ADD”或“NA”或“both”之外还有其他任何内容,我如何让我的函数返回false? 感谢任何帮助。

更新: 在我的代码中,我只是在我可能想到的所有那些我不需要的值中添加了一个NOT条件。我需要一个更有效的解决方案。

另外:我的true()条件是ADD必须始终存在,而NA是可选的。除必需的“ADD”和可选的“NA”之外,不能存在其他值。 例如:

  1. {ADD}返回true
  2. {ADD,NA}返回true
  3. {ADD,DEL}返回false
  4. {DEL,YAK}返回false等
  5. 以下代码段没有此检查,我正在寻找最不详细的最佳解决方案。

    这是片段。字符串列表作为参数传递给isAddOrNa()。

    private boolean isAddOrNa(List<AllActions> allActions) {
        Set<String> actions = new HashSet<>();
        for (com.demo.Action action : allActions) {
            actions.add(action.getName());
        }
        return ((actions.size() <= 2) && (actions.contains("ADD") || actions.contains("NA"))) &&
                !(actions.contains("DEL") || actions.contains("CHG") || actions.contains("BLAH") || actions.contains("YAK"));
    }
    

6 个答案:

答案 0 :(得分:1)

private boolean isAddOrNa(List<AllActions> allActions) {
    Set<String> actions = new HashSet<>();
    for (com.demo.Action action : allActions) {
        actions.add(action.getName());
    }
    return actions.contains("ADD") && ((actions.contains("NA") && actions.size() ==2) || (!actions.contains("NA") && actions.size() ==1));
}

最大限度地减少条件。这有帮助吗?

答案 1 :(得分:0)

 import com.google.common.collect.Sets;
 String add= "ADD";
 String NA ="NA"
 final Set<String> Strings = Sets.newHashSet("ADD","DEL","CHG","DEL","NA","ADD","BLAH","YAK");

 if (!((Strings .contains(add))||(Strings .contains(NA )))){
 return false;
 }

根据你的要求,

if(String.contains(add)){
    if(String.contains(NA)){
        return true;
          }
     }
 return false;

答案 2 :(得分:0)

如果(hashSet.contains(&#34; ADD&#34)||(hashset.contains(&#34; NA&#34))    返回false;

使用它谢谢

答案 3 :(得分:0)

您的问题的解决方案是检查输入集中“NA”和“ADD”的其他内容。这可以通过使用验证集添加输入集来实现,同时如果输入集中没有找到新元素,则会出现错误,否则为true:

            import java.util.HashSet;
            import java.util.Set;

            public class HashSetTheory {

                public static void main(String args[]){

                    String[] sourceData = {"ADD","DEL","CHG","DEL","NA","ADD","BLAH","YAK" }; // Output is false
                    //String[] sourceData = {"ADD","ADD","ADD","NA","NA","ADD","NA","ADD" };  // Output is true
                    //String[] sourceData = {"ADD","ADD","ADD" };  // Output is true
                    //String[] sourceData = { };  // Output is false
                    //String[] sourceData = { "NA" , "Something"};  // Output is false

                    Set<String> myRepo = new HashSet<>();

                    // Populating the set with input data
                    for(String data : sourceData){
                        myRepo.add(data);
                    }

                    Set<String> check = new HashSet<>();
                    check.add("NA");
                    check.add("ADD");

                    System.out.println("==>" + validateData(myRepo,check) );

                }

                public static boolean validateData(Set<String> myRepo,Set<String> check){
                    boolean retVal = true;

                    if(myRepo.size()==0){
                        return false;
                    }

                    for (String s : myRepo) {
                        // Adding an element in set returns false if it already exists
                        if(check.add(s)){
                            //This check is pass only if the set has some new item in it
                            retVal = false;
                            break;
                        }
                    }

                    return retVal;
                } 

            }

答案 4 :(得分:0)

根据我的理解,您需要一种Set的验证方法。例如,如果JSON中的列表只是{“ADD”,“NA”,“ADD”},那就没问题了,你的方法应该返回true。如果列表中还有其他值,例如问题中的示例,则应返回false。

当你知道如何时,这并不复杂:

private static boolean setHasOnlySpecifiedValues(
        Set<String> specifiedValues, Set<String> setToCheck) {
    // we check setToCheck by removing specifiedValues and seeing if there are any
    // values left: however, the caller may be surprised if we remove elements from
    // the set passed to us, so work on a copy instead
    Set<String> workingCopy = new HashSet<>(setToCheck);
    workingCopy.removeAll(specifiedValues);
    if (workingCopy.isEmpty()) { // there were only specified values
        return true;
    } else {
        System.out.println("The set contained other values: " + workingCopy);
        return false;
    }
}

Java集合框架中的removeAll方法是所谓的集合操作之一(不是因为它适用于集合,它也适用于列表和映射)。您可以将其视为设置差异操作的实现。

让我们试试这个方法:

    List<String> fromJson 
            = Arrays.asList("ADD", "DEL", "CHG", "DEL", "NA", "ADD", "BLAH", "YAK");
    Set<String> setToCheck = new HashSet<>(fromJson);
    Set<String> specifiedValues = new HashSet<>(Arrays.asList("ADD", "NA"));
    // or in Java 9: Set<String> specifiedValues = Set.of("ADD", "NA");

    boolean ok = setHasOnlySpecifiedValues(specifiedValues, setToCheck);
    System.out.println("The result of the check is: " + ok);

打印:

The set contained other values: [YAK, CHG, DEL, BLAH]
The result of the check is: false

最后,您可能不希望检查方法中包含System.out.println()语句。我现在把它放在那里,这样你就可以看到removeAll()的确切结果。

答案 5 :(得分:-1)

除了我有多大你的名单或你的意图是什么,但是根据你提供的内容,一个天真的解决方案就是在HashSet上对这些选定的单词进行3次搜索追踪的命中。然后将它与HashSet的size()进行比较。

相关问题