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 6940829
In Process

The Archive Base Latest Questions

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

I am having trouble using Iterator in java. it=myHash.iterator(); while (it.hasNext()) if (it.next() satisfying

  • 0

I am having trouble using Iterator in java.

it=myHash.iterator();
 while (it.hasNext()) 
        if (it.next() satisfying something) 
            do something

 while (it.hasNext()) 
        if (it.next() satisfying something) 
            it.remove();

I am trying to iterate hashset twice, and the first loop making it.hasNext() return false. How to i resolve this?

I tried even adding the edit as you guys suggested, still not working…

  • 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-27T12:48:13+00:00Added an answer on May 27, 2026 at 12:48 pm

    You’re reusing the same iterator – and that’s been invalidated by adding new items to the set.

    You should call iterator() again on the set:

    it = set.iterator();
    

    You can’t reset the existing iterator

    EDIT: Here’s some sample code which shows this working:

    import java.util.*;
    
    public class Test {
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
    
            set.add("food");
            set.add("bad");
            set.add("hungry");
            set.add("neighbour");
    
            Iterator<String> it = set.iterator();
    
            // Remove any string longer than 4
            while (it.hasNext())
            {
                if (it.next().length() > 4)
                {
                    it.remove();
                }
            }
    
            set.add("new long text");
            set.add("x");
    
            // Remove any string shorter than 4
            it = set.iterator();
    
            while (it.hasNext())
            {
                if (it.next().length() < 4)
                {
                    it.remove();
                }
            }
    
            // Dump the results
            for (String x : set)
            {
                System.out.println(x);
            }
        }
    }
    

    This gives the results “new long text” and “food”.

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

Sidebar

Related Questions

I am having trouble using the link_to_remote method while rendering a layout. For example
I am having trouble using srand. i am trying to generate a random number
Im having trouble using a .NET COM in vb6, It compiles ok and I
I am using mssql and am having trouble using a subquery. The real query
I am having trouble using the attribute XPath Selector in ElementTree, which I should
I'm having trouble using the flowlayoutPanel in a C# winform application. What I basically
I've been having trouble using cucumber and webrat to test authlogic-openid authentication in a
I'm having trouble using os.utime to correctly set the modification time on the mac
I am having trouble using EnumSet on the client side. I get this runtime
I'm new to development using Xcode, and am having trouble using the built-in Leaks

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.