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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:38:24+00:00 2026-05-25T09:38:24+00:00

In many algorithm, you’re supposed to iterate over a set of elements, while the

  • 0

In many algorithm, you’re supposed to iterate over a set of elements, while the set is not empty.

Since you might change the set while iterating it, you typically pull an element out of the set, and then do the iteration, possibly adding or removing elements to or from the set. Here is a typical Java code to do that.

Set<Integer> possibleFactors = Sets.newHashSet(2,3,4,5,6,7,8,100);
while (!possibleFactors.isEmpty()) {
    int factor = possibleFactors.iterator().next();
    for (int i=1;i<10;i++) possibleFactors.remove(i*factor);
}

edit: As asked in the comments, I’ll give a better example. I’m iterating through files the user have chosen, and I’m filtering them by checking the permissions of each item. However, as an optimization, if the user don’t have permission for a certain directory, I’ll remove all the files in it from the set.

Set<Path> input = Sets.newHashSet(userSelectedPaths);
while (!input.isEmpty()) {
    Path path = input.iterator.next();
    input.remove(path);
    if (!expensivePermissionCheck(path)) {
        input.removeAll(path.getFiles());
    } else {
        processPath(path);
    }
}

However the first line in the loop looks weird. It creates a superfluous Iterable objects, when all I want is an arbitrary element from the set, I don’t care in which order.

Except the performance, it looks kind of weird, and less readable.

Is there a better alternative? Maybe a different structure altogether?

edit: maybe a better formulation would be “How do I pop arbitrary element from a set?”

  • 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-25T09:38:24+00:00Added an answer on May 25, 2026 at 9:38 am

    The only access methods for the Set interface are via the iterator() method or the toArray() method.

    If you have a SortedSet you can use first() or last() method to access one item directly.

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

Sidebar

Related Questions

I have this algorithm, but I am not too keen on the many if-statements.
Some Genetic Algorithm frameworks, such as http://www.aforgenet.com/ requires many parameters, such as mutation rate,
I am currently trying to write an algorithm that determines how many bits are
Many hex editors, such as Hex Workshop, can even read large-sized files while keeping
is there any STL algorithm or a standard way of finding how many occurences
There are many algorithms to evaluate expressions, for example: By Recursive Descent Shunting-yard algorithm
People can belong to one or many groups. What is a good algorithm to
we use md5 as a hashing algorithm in many parts of our code. security
I'm looking for an algorithm that will evenly distribute 1 to many items into
According to this Socket FAQ article , Nagle's algorithm is one of many algorithms

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.