Note that there are some explanatory texts on larger screens.

plurals
  1. POConsistency of contains method on a HashSet and HashMap
    primarykey
    data
    text
    <p>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</p> <pre><code>public static void main(String[] args) { HashSet&lt;String&gt; lookup=new HashSet&lt;String&gt;(); HashMap&lt;Integer,String&gt; findup=new HashMap&lt;Integer,String&gt;(); 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&lt;String&gt; 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); } </code></pre> <p>Output NOT found In map true</p> <p>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</p> <pre><code> lookup.remove("Alltokens[5]"); if(lookup.containsAll(tok)) </code></pre> <p>//This will be true now where as it is false if a value already present is removed</p> <p>May be it has got to do something with keys in HashMaps and no keys in HashSet.Can you please explain how do they work?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload