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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:49:52+00:00 2026-05-30T17:49:52+00:00

Still learning the ropes with Groovy, this problem has tripped me up since last

  • 0

Still learning the ropes with Groovy, this problem has tripped me up since last night. Not sure why it’s throwing concurrentmod exception…(Java 1.6, Groovy 1.8.4)

I have a list of keys…
[1,2,3,4,5,6,7,8,9,10,11,12,13]

I collate the list using a custom function partitionList(keys,3) I got from here (can’t use java.List.Collate, not on 1.8.6)

Now I’ve got a list of lists…
[[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13]]

If the number of sub-lists created is odd, I remove the last sub list [12,13] and redistribute its keys amongst the other sub lists starting in order, creating…

[[0,1,2,12],[3,4,5,13],[6,7,8],[9,10,11]]

The exception occurs when iterating the values of the last sub list. Not sure why since I’m iterating a list and changing an entirely different list in that loop…

UPDATE:

Interesting….if I don’t use the paritionList() function, replacing
def keyRanges = partitionList( keys, 3)
with an explicit list of lists…
def keyRanges = [[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13]]
the problem disappears. So I’m lead to believe the partitionList() function is doing something that’s causing the exception

class CollateListTest {

    static main(args) {    
        def keys = (0..13).toList()

        //partition the keys into a list of lists 
        def keyRanges = partitionList( keys, 3)
        println 'Key range sets...'
        for( keyRange in keyRanges)
            println keyRange

        //if odd number of partitions, 
        //remove last sub list and redistribute values over other lists
        if( (keyRanges.size() % 2) != 0){
            def lastKeyRange = keyRanges.remove( keyRanges.size() - 1 )
            println 'removed range: ' + lastKeyRange

                    // ** EXCEPTION HERE **         
            lastKeyRange.eachWithIndex{ k, index ->
                println 'adding: ' + k
                keyRanges[ index % keyRanges.size()].add( k)
            }
        }
    }

    //from stackoverflow.com/questions/2924395/
    static def partitionList(list, size) {
        def partitions = []
        int partitionCount = list.size() / size

        partitionCount.times { partitionNumber ->
            def start = partitionNumber * size
            def end = start + size - 1
            partitions << list[start..end]
        }

        if (list.size() % size) partitions << list[partitionCount * size..-1]
        return partitions
    }
}
  • 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-30T17:49:53+00:00Added an answer on May 30, 2026 at 5:49 pm

    The partitionList method you’re using splits up the list with List.getAt(Range). This returns a view into the original list, but doesn’t copy the data, so any modifications to the sublist also affect the original.

    This means that lastKeyRange shares data with keyRanges, and that adding to one of the sublists indirectly affects the sublist you are iterating over. Rather than modifying the sublist, just create a new one. For example:

    if( (keyRanges.size() % 2) != 0){
        def lastKeyRange = keyRanges.remove( keyRanges.size() - 1 )
        println 'removed range: ' + lastKeyRange
    
        lastKeyRange.eachWithIndex{ k, index ->
            println 'adding: ' + k
            def newRange = []
            newRange.addAll(keyRanges[ index % keyRanges.size()])
            newRange.add(k)
            keyRanges[ index % keyRanges.size()] = newRange
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is really a dumb newbie question, but I am still learning the ropes
I'm still learning lisp, so, when I came across this problem, it confused me.
Okay I'm still learning the ropes with the Entity Framework and I came across
I'm still learning the ropes of Git (love it!) but the other day I
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
I'm still learning ASP.NET and I often see code like this throughout parts of
I'm still learning about REST and, in my test, came up with this scenario
I'm still learning the ropes of OLAP, cubes, and SSAS, but I'm hitting a
Ok, so I'm still learning the ropes of C++ here so I apologize if
I'm still learning the WPF ropes, so if the following question is trivial or

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.