title: How to iterate over Set and add elements to it in java?
body: <p>I want to iterate over a Set and if some condition meet I want to add elements to it. 
  While I am doing this, I am getting "ConcurrentModificationException". 
  When I looked for the answer, I found that in case of listIterator we have add() and remove() method but I can't use list as I also have to take care of duplicates.</p>
  
  <p>Please suggest a way to achieve this.</p>
  
  <p>Edit:</p>
  
  <pre><code>int[] A = {1,2,3,4,5,10,6,7,9};
  Set&lt;Integer&gt; s = new HashSet&lt;&gt;();
  s.add(1);
  Iterator i = s.iterator();
  while(i.hasNext()){
        int temp = i.next();
        int x = next element of array A;
        if(x%2==0){
            s.add(temp*x);
            }
         }
  </code></pre>
  
  <p>But it is throwing ConcurrentModificationException.</p>