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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:38:52+00:00 2026-06-15T23:38:52+00:00

I stumbled over this odd bug. Seems like Collections.sort() does not modify the sorted

  • 0

I stumbled over this odd bug. Seems like Collections.sort() does not modify the sorted list in a way that enables a detection of concurrent modifications when also iterating over the same list. Example code:

    List<Integer> my_list = new ArrayList<Integer>();

    my_list.add(2);
    my_list.add(1);

    for (Integer num : my_list) {

        /*
         * print list
         */
        StringBuilder sb = new StringBuilder();
        for (Integer i : my_list)
            sb.append(i).append(",");
        System.out.println("List: " + sb.toString());

        /*
         * sort list
         */
        System.out.println("CurrentElement: " + num);
        Collections.sort(my_list);
    }

outputs

List: 2,1,
CurrentElement: 2
List: 1,2,
CurrentElement: 2

One would expect a ConcurrentModificationException, but it is not being raised and the code works although it shouldn’t.

  • 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-06-15T23:38:53+00:00Added an answer on June 15, 2026 at 11:38 pm

    Update: This answer was written for Java 6. With JDK 8u20’s Collection.sort now deferring to List.sort, Java 8+ now does throw an exception, although as pointed out by Brian Roach, it still doesn’t provide any guarantee to that effect.

    Why would it throw ConcurrentModificationException when you are not adding/removing elements from your collection while iterating?

    Note that ConcurrentModificationException would only occur when a new element is added in to your collection or remove from your collection while iterating. i.e., when your Collection is Structurally modified.

    (Structural modifications are those that change the size of this list,
    or otherwise perturb it in such a fashion that iterations in progress
    may yield incorrect results.)

    sort wouldn’t structurally modify your Collection, all it does is modify the order.
    Below code would throw ConcurrentModificationException as it add’s an extra element into the collection while iterating.

    for(Integer num : my_list) {
        my_list.add(12);
        }
    

    If you look at the source of the sort method in the Collections class, it’s not throwing ConcurrentModificationException.

    This implementation dumps the specified list into an array, sorts the
    array, and iterates over the list resetting each element from the
    corresponding position in the array. This avoids the n2 log(n)
    performance that would result from attempting to sort a linked list in
    place.

    public static <T extends Comparable<? super T>> void sort(List<T> list) {
            Object[] a = list.toArray();
            Arrays.sort(a);
            ListIterator<T> i = list.listIterator();
            for (int j=0; j<a.length; j++) {
                i.next();
                i.set((T)a[j]);
            }
        }
    

    Extract from the book java Generics and Collections:

    The policy of the iterators for the Java 2 collections is to fail
    fast, as described in Section 11.1: every time they access the backing
    collection, they check it for structural modification (which, in
    general, means that elements have been added or removed from the
    collection). If they detect structural modification, they fail
    immediately, throwing ConcurrentModificationException rather than
    continuing to attempt to iterate over the modified collection with
    unpredictable results.

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

Sidebar

Related Questions

While refactoring some code I stumbled over this oddity. It seems to be impossible
The last week I stumbled over this paper where the authors mention on the
I've recently stumbled over this expression: True == False in (False,) It evaluates to
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
in another topic, I've stumbled over this very elegant solution by Darin Dimitrov to
I stumbled over node.js sometime ago and like it a lot. But soon I
I stumbled over a curious bug, I think: I tried to read 512 as
In code reviews I stumbled over this java-pattern to avoid NPE's: value = (null
I just converted a code snippet from VB.NET to C# and stumbled over this
This morning I stumbled over a weird issue in Visual Studio's Windows Forms designer.

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.