Note that there are some explanatory texts on larger screens.

plurals
  1. POcode to prove that hashMap is fail-Safe
    primarykey
    data
    text
    <p>Question Edited :<br> HashSet and HashMap are fail-fast(but that is not gauranteed) as mentioned in code :</p> <pre><code>void goHashSet() { Set set = new HashSet(); for (int i = 1; i &lt;= 10; i++) { set.add(i); } Iterator i = set.iterator(); while (i.hasNext()) { // set.add(16);//Exception in thread "main" // java.util.ConcurrentModificationException System.out.println("HashSet &gt;&gt;&gt; itertor &gt;&gt;&gt;" + i.next()); } } </code></pre> <p>Now, i want the example and the collections which are fail-safe<br> as much as i know: ConcurrentHashMap,CopyOnWriteArrayList are fail-safe..but how to code it to show that they are fail-safe</p> <p><strong>Edit and Undestanding of what I want and how I achieved it :</strong></p> <p>if we use HashMap</p> <pre><code>void goHashMap() { Map mp = new HashMap(); for (int i = 19; i &lt;= 24; i++) { mp.put(i, "x"); } Set setKeys = mp.keySet(); Iterator i = setKeys.iterator(); while (i.hasNext()) { // mp.put(499, "x");// Exception in thread "main" // java.util.ConcurrentModificationException System.out.println("HashMap &gt;&gt;&gt; itertor &gt;&gt;&gt;" + i.next()); } } </code></pre> <p>we get ConcurrentMException</p> <p>but the same code is done with ConcurrentHashMap, no error is there (non-multithreading enviro)</p> <pre><code>void goConcurrentHashMap() { Map mp = new ConcurrentHashMap(); for (int i = 19; i &lt;= 24; i++) { mp.put(i, "x"); } Set setKeys = mp.keySet(); Iterator i = setKeys.iterator(); while (i.hasNext()) { mp.put(499, "x"); System.out.println("HashConcurrentMap &gt;&gt;&gt; itertor &gt;&gt;&gt;" + i.next()); } } </code></pre> <p>whats more : in multithreading enviro the ConcurrentHashmap might get fail-fast and throw exception CME</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.
    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