java Set.contains() 如何决定它是否是一个子集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17824140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 15:08:22  来源:igfitidea点击:

How Set.contains() decides whether it's a subset or not?

javaiteratorsetsubsethashset

提问by Zoe

I expect the following code would give me a subset and a complementary set.

我希望下面的代码会给我一个子集和一个补充集。

But actually, the result shows that "Error: This is not a subset!"

但实际上,结果显示“错误:这不是子集!”

What it.next() get and how to revise my code to get the result I want? Thanks!

it.next() 得到什么以及如何修改我的代码以获得我想要的结果?谢谢!

package Chapter8;

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

public class Three {
    int n;
    Set<Integer> set = new HashSet<Integer>();

    public static void main(String args[]) {
        Three three = new Three(10);
        three.display(three.set);
        Set<Integer> test = new HashSet<Integer>();
        Iterator<Integer> it = three.set.iterator();
        while(it.hasNext()) {
            test.add(it.next());
            three.display(test);
            three.display(three.complementarySet(test));
        }

    }

    boolean contains(Set<Integer> s) {
        if (this.set.contains(s))
            return true;
        else 
            return false;
    }

    Set<Integer> complementarySet(Set<Integer> s) {
        if(this.set.contains(s)){
            Set<Integer> result = this.set;
            result.removeAll(s);
            return result;
        }
        else {
            System.out.println("Error: This is not a subset!");
            return null;
        }
    }

    Three() {
        this.n = 3;
        this.randomSet();
    }

    Three(int n) {
        this.n = n;
        this.randomSet();
    }

    void randomSet() {
        while(set.size() < n) {
            set.add((int)(Math.random()*10));
        }
    }

    void display(Set<Integer> s) {
        System.out.println("The set is " + s.toString());
    }
}

采纳答案by morgano

Your problem is in this part:

你的问题在这部分:

set.contains(s)

that doesn't do what you think it does, it doesn't take as an argument another Setto see if its members are contained in the firstset. It rather looks if the argument passed it is in the Set.

这不会做你认为它做的事情,它不会将另一个作为参数Set来查看它的成员是否包含在第一个set. 而是查看传递的参数是否在 Set 中。

You need to iterate over the "contained" set and use set.contains(element)for each element in the contained set.

您需要遍历“包含”集合并set.contains(element)用于包含集合中的每个元素。

回答by gjain

You probably want to use set.containsAll(Collection <?> C)for checking if Collection(Set, in this case) is a subset of 'set'. From the docs: http://docs.oracle.com/javase/7/docs/api/java/util/Set.html#containsAll(java.util.Collection)

您可能想set.containsAll(Collection <?> C)用于检查 Collection(Set, in this case) 是否是“set”的子集。从文档:http: //docs.oracle.com/javase/7/docs/api/java/util/Set.html#containsAll(java.util.Collection)

boolean containsAll(Collection c)

Returns true if this set contains all of the elements of the specified collection. If the specified collection is also a set, this method returns true if it is a subset of this set.

布尔值 containsAll(Collection c)

如果此集合包含指定集合的​​所有元素,则返回 true。如果指定的集合也是一个集合,如果它是这个集合的子集,则此方法返回 true。