Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:48:29+00:00 2026-05-27T08:48:29+00:00

I was trying to filter specific elements from an Iterator object, and I can

  • 0

I was trying to filter specific elements from an Iterator object, and I can remove the false elements via iterator.remove() method.

Here is my method stub:

    private static Iterator<A> process(Iterator<A> iter, Per p) {
        while(iter.hasNext()){
          A o = iter.next();
          if(p.per(o)){
             iter.remove();
          }
        }
        return iter;
    }

I can remove the un-related elements, but when I look into the java api for Iterator.reomve(), it’s saying:

void remove():
Removes from the underlying collection the last element returned by the iterator (optional operation). 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.

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?

Thanks for you all in advance for help.

And the whole code I was compiling is below for your reference:

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<A> list = new ArrayList<A>(){{
            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<A> iter = list.iterator();
        iter = process(iter, it.new Per());
        System.out.println("");
    }

    private static Iterator<A> process(Iterator<A> 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;
            }
        }
    }   

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:48:30+00:00Added an answer on May 27, 2026 at 8:48 am

    It means that per the contract of the API, the remove() method is not required to be implemented / available on all implementations. This is because on some implementations, it simply may not make sense – or would be infeasible to implement. A good example would be the iterator returned on a read-only collection – such as the one returned by Collections.unmodifiableCollection().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to filter a list of elements via ':not()', and jQuery seems to
I'm trying to filter some XML in JavaScript using E4X and have some specific
I am trying to filter an IEnumerable object of the duplicate values, so I
I'm trying to filter a table from an alphabetical <select> input with jQuery. I
I am trying to filter for an object that has a Title field, and
I'm looking for an efficient way to filter a specific color from a bitmapData
I'm trying to obtain a specific li element from a unordered list. I'm doing
I am trying to programmatically add and remove application shortcuts from the app drawer.
I am trying to match specific span-tags from an HTML source. The lang-attribute and
I am trying to filter table results from Table B by clicking on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.