Set <Set> Java中的平等

ruc*_*rni 8 java string contains equals set

我有一个返回的方法Set<Set<String>>.在我的测试中,我试图Set使用contains()方法检查预期的s是否存在.

例如. input = "cat", "dog", "god"

output = [[cat], [dog, god]]

现在,如果我这样做output.contains(new HashSet<>(Arrays.asList("cat")))会返回true.

但如果我这样做output.contains(new HashSet<>(Arrays.asList("dog", "god")))就会回来false.

根据我的理解,它应该true在两种情况下都返回.

我在这里错过了什么?

public class AnagramGroups {
     public Set<Set<String>> group(Set<String> words) {
         Set<Set<String>> groups = new HashSet<>();
         for(String word: words) {
             findAndAdd(word, groups);
         }
         return groups;
     }

     private void findAndAdd(String word, Set<Set<String>> groups) {
         for(Set<String> group: groups) {
             boolean found = false;
             for(String str: group) {
                 if(isAnagram(str, word)) {
                     found = true;
                 }
                 break;
             }
             if(found) {
                 group.add(word);
                 return;
             }
         }
         Set<String> set = new HashSet<>();
         set.add(word);
         groups.add(set);
     }

     private boolean isAnagram(String str, String word) {
         Set<Character> characters = new HashSet<>();
         for(char c: str.toCharArray()) {
             characters.add(c);
         }
         for(char c: word.toCharArray()) {
             if(!characters.contains(c)) {
                 return false;
             }
             characters.remove(c);
         }
         return characters.isEmpty();
     }

     public static void main(String[] args) {
         Set<Set<String>> groups = new AnagramGroups()
             .group(new HashSet<>(Arrays.asList("cat", "god", "dog")));
         System.out.println(groups);

         Set set1 = new HashSet<>(Arrays.asList("cat"));
         Set set2 = new HashSet<>(Arrays.asList("god", "dog"));
         System.out.println(groups.contains(set1));
         System.out.println(groups.contains(set2));

         groups.add(new HashSet<>(Arrays.asList("god", "dog")));
         System.out.println(groups);
     }
}
Run Code Online (Sandbox Code Playgroud)

Era*_*ran 4

问题出在您的方法中,您正在改变外部( ) 的findAndAdd元素 ( ) ,从而更改其. 因此,无法找到中存在的a ,因为它在错误的存储桶中查找它(与新的 匹配),而不是在添加它的存储桶中查找(与原始的 匹配)。groupSetgroupshashCode()groups.contains(set2)SetgroupshashCode()hashCode()

您可以通过在修改代码之前删除group Setfrom来修复代码groups,然后重新添加它。

更改您的代码:

 private void findAndAdd(String word, Set<Set<String>> groups) {
     for(Set<String> group: groups) {
         boolean found = false;
         for(String str: group) {
             if(isAnagram(str, word)) {
                 found = true;
             }
             break;
         }
         if(found) {
             group.add(word);
             return;
         }
     }
     Set<String> set = new HashSet<>();
     set.add(word);
     groups.add(set);
 }
Run Code Online (Sandbox Code Playgroud)

到:

 private void findAndAdd(String word, Set<Set<String>> groups) {
     for(Set<String> group: groups) {
         boolean found = false;
         for(String str: group) {
             if(isAnagram(str, word)) {
                 found = true;
             }
             break;
         }
         if(found) {
             groups.remove(group);
             group.add (word);
             groups.add(group);
             return;
         }
     }
     Set<String> set = new HashSet<>();
     set.add(word);
     groups.add(set);
 }
Run Code Online (Sandbox Code Playgroud)

当我尝试你的代码并进行更改时,我遇到了true两种情况。

输出:

[[cat], [god, dog]]
true
true
[[cat], [god, dog]]
Run Code Online (Sandbox Code Playgroud)