title: what does it mean when java.util.Iterator.remove() method is 'optional operation'?
body: <p>I was trying to filter specific elements from an Iterator object, and I can remove the false elements via <code>iterator.remove()</code> method.</p>
  
  <p>Here is my method stub:</p>
  
  <pre><code>    private static Iterator&lt;A&gt; process(Iterator&lt;A&gt; iter, Per p) {
          while(iter.hasNext()){
            A o = iter.next();
            if(p.per(o)){
               iter.remove();
            }
          }
          return iter;
      }
  </code></pre>
  
  <p>I can remove the un-related elements, but when I look into the java api for Iterator.reomve(), it's saying:</p>
  
  <blockquote>
    <p>void remove():
    Removes from the underlying collection the last element returned by the iterator (<strong>optional operation</strong>). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.</p>
  </blockquote>
  
  <p>I just confused about why it's an optional operation? Is it a warning to let programmer be more careful when using the remove() method? or it has other meanings?</p>
  
  <p>Thanks for you all in advance for help.</p>
  
  <p>And the whole code I was compiling is below for your reference:</p>
  
  <pre><code>package com.test.iterator;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  public class IteratorTest {
  
      public static void main(String[] args) {
          final IteratorTest it = new IteratorTest();
  
          List&lt;A&gt; list = new ArrayList&lt;A&gt;(){{
              add(it.new A(10));
              add(it.new A(11));
              add(it.new A(12));
              add(it.new A(13));
              add(it.new A(16));
          }};
  
          Iterator&lt;A&gt; iter = list.iterator();
          iter = process(iter, it.new Per());
          System.out.println("");
      }
  
      private static Iterator&lt;A&gt; process(Iterator&lt;A&gt; iter, Per p) {
          while(iter.hasNext()){
              A o = iter.next();
              if(p.per(o)){
                  iter.remove();
              }
          }
          return iter;
      }
  
      class A{
          private String name;
          private int age;
          public A(int i){
              this.age = i;
              this.name = i + ",wang";
          }
      }
      class Per{
          public boolean per(A a){
              if(a.age % 2 == 0){
                  return true;
              }else{
                  return false;
              }
          }
      }   
  
  }
  </code></pre>