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

  • SEARCH
  • Home
  • 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 6348777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:29:19+00:00 2026-05-24T21:29:19+00:00

I need to update some fixed-priority elements in a PriorityQueue based on their ID.

  • 0

I need to update some fixed-priority elements in a PriorityQueue based on their ID. I think it’s quite a common scenario, here’s an example snippet (Android 2.2):

for (Entry e : mEntries) {
    if (e.getId().equals(someId)) {
        e.setData(newData);
    }
}

I’ve then made Entry “immutable” (no setter methods) so that a new Entry instance is created and returned by setData(). I modified my method into this:

for (Entry e : mEntries) {
    if (e.getId().equals(someId)) {
        Entry newEntry = e.setData(newData);
        mEntries.remove(e);
        mEntries.add(newEntry);
     }
}

The code seems to work fine, but someone pointed out that modifying a queue while iterating over it is a bad idea: it may throw a ConcurrentModificationException and I’d need to add the elements I want to remove to an ArrayList and remove it later. He didn’t explain why, and it looks quite an overhead to me, but I couldn’t find any specific explanation on internet.

(This post is similar, but there priorities can change, which is not my case)

Can anyone clarify what’s wrong with my code, how should I change it and – most of all – why?

Thanks,
Rippel


PS: Some implementation details…

PriorityQueue<Entry> mEntries = new PriorityQueue<Entry>(1, Entry.EntryComparator());

with:

public static class EntryComparator implements Comparator<Entry> {
    public int compare(Entry my, Entry their) {
        if (my.mPriority < their.mPriority) {
            return 1;
        }
        else if (my.mPriority > their.mPriority) {
            return -1;
        }
        return 0;
    }
}
  • 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-24T21:29:20+00:00Added an answer on May 24, 2026 at 9:29 pm

    This code is in the Java 6 implementation of PriorityQueue:

    private class Itr implements Iterator<E> {
      /**
       * The modCount value that the iterator believes that the backing
       * Queue should have.  If this expectation is violated, the iterator
       * has detected concurrent modification.
       */
      private int expectedModCount = modCount;
    
      public E next() {
        if(expectedModCount != modCount) {
          throw new ConcurrentModificationException();
        }
    
    
      }
    
    }
    

    Now, why is this code here? If you look at the Javadoc for ConcurrentModificationException you will find that the behaviour of an iterator is undefined if modification occurs to the underlying collection before iteration completes. As such, many of the collections implement this modCount mechanism.

    To fix your code

    You need to ensure that you don’t modify the code mid-loop. If your code is single threaded (as it appears to be) then you can simply do as your coworker suggested and copy it into a list for later inclusion. Also, the use of the Iterator.remove() method is documented to prevent ConcurrentModificationExceptions. An example:

    List<Entry> toAdd = new ArrayList<Entry>();
    Iterator it = mEntries.iterator();
    while(it.hasNext()) {
      Entry e = it.next();
    
      if(e.getId().equals(someId)) {
        Entry newEntry = e.setData(newData);
        it.remove();
        toAdd.add(newEntry);
      }
    }
    mEntries.addAll(toAdd);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a case where I need to update a jqgrid based on some
I need to use scp update some directory at another server. It is similar
I need to execute a select and then update some of the rows in
I need some SQL to update a record in a database if it exists
I need to update a field (which is currently empty) based on a match
I've inherited some rather large static HTML files that need to be fixed up
Consider a spreadsheet which performs some computations based on a fixed value (in the
I need to update a combobox with a new value so it changes the
I need to update a record in a database with the following fields [ID]
I need to update a group of cells by inserting the same two characters

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.