Consistency of contains method on a HashSet and HashMap

bl3*_*l3e 0 java hashmap hashset

Why does containsAll method on a HashSet does not remain consistent if remove is called on the Set whereas a containsValue method on a HashMap remains consistent after a value is removed After a value is removed from a HashSet containsAll returns false even if all values were present where as in case of HashMap the containsValue method returns correct value

public static void main(String[] args)
    {

    HashSet<String> lookup=new HashSet<String>();
    HashMap<Integer,String> findup=new HashMap<Integer,String>();

    String[] Alltokens={"This","is","a","programming","test","This","is","a","any","language"};
    for(String s:Alltokens)
        lookup.add(s);

    String[] tokens={"This","is","a"};
    Collection<String> tok=Arrays.asList(tokens);

    lookup.remove(Alltokens[5]); 
     if(lookup.containsAll(tok))
       System.out.print("found");
    else    
        System.out.print("NOT found");

    Integer i=0;
    for(String s:Alltokens)
        findup.put(i++,s);
    boolean found=true;
    findup.remove(Alltokens[0]);
        findup.remove(5);               //Edit : trying to remove value whose key is 5
    for(String s:tokens)
        if(!findup.containsValue(s))
            found=false;
    System.out.print("\nIn map " + found);
}
Run Code Online (Sandbox Code Playgroud)

Output NOT found In map true

Is there a way to keep containsAll consistent if remove method is called on the HashSet? Also if a value that was not present in the set is passed to remove method.ContainsAll remains consistent

        lookup.remove("Alltokens[5]");
    if(lookup.containsAll(tok))
Run Code Online (Sandbox Code Playgroud)

//This will be true now where as it is false if a value already present is removed

也许它必须用HashMaps中的键做一些事情而HashSet中没有键.你能解释一下它们是如何工作的吗?

Joa*_*uer 5

Map.remove(Object)删除基于密钥的映射.

由于你使用Integer对象作为键(put(i++, s)),你打电话时什么也不会删除findup.remove(Alltokens[0])!检查其返回值以查看它将返回false.