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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:44:12+00:00 2026-06-14T01:44:12+00:00

I want to perform a copy and get two different objects so that I

  • 0

I want to perform a copy and get two different objects so that I can work on the copy without impacting the original.

I have this code (groovy 2.0.5):

def a = [[1,5,2,1,1], ["one", "five", "two", "one", "one"]]
def b = a
b.add([6,6,6,6,6,6])
println a
println b

that produces:

[[1, 5, 2, 1, 1], [one, five, two, one, one], [6, 6, 6, 6, 6, 6]]
[[1, 5, 2, 1, 1], [one, five, two, one, one], [6, 6, 6, 6, 6, 6]]

seems like b and a are actually the same object

I can fix it in this way:

def a = [[1,5,2,1,1], ["one", "five", "two", "one", "one"]]
def b = []

a.each {
    b.add(it)
}

b.add([6,6,6,6,6])
println a
println b

that produces my wanted result:

[[1, 5, 2, 1, 1], [one, five, two, one, one]]
[[1, 5, 2, 1, 1], [one, five, two, one, one], [6, 6, 6, 6, 6]]

But now look at this, where I want the original object and a copy with unique and sorted elements:

def a = [[1,5,2,1,1], ["one", "five", "two", "one", "one"]]
def b = a

b.each {
    it.unique().sort()
}

println a
println b

that produces:

[[1, 2, 5], [five, one, two]]
[[1, 2, 5], [five, one, two]]

If I try the same fix this time it doesn’t work:

def a = [[1,5,2,1,1], ["one", "five", "two", "one", "one"]]
def b = []

a.each { 
    b.add(it)
}

b.each {
    it.unique().sort()
}

println a
println b

that still produces:

[[1, 2, 5], [five, one, two]]
[[1, 2, 5], [five, one, two]]

What’s going on ?

  • 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-14T01:44:13+00:00Added an answer on June 14, 2026 at 1:44 am

    Just calling b = a sets b to being the same instance of the list (containing the same list instances) as a

    Calling the second method with the a.each { b.add(it) } means b points to a different instance of List, but the contents of b are the same instances of lists as in a

    What you need is something like:

    def b = a*.collect()
    
    b.each {
      it.unique().sort()
    }
    

    So the a*.collect() makes a new instance of a list for each list in a

    You can also do this in one line:

    def b = a*.unique( false )*.sort( false ) 
    

    Passing false to unique and sort stop those method changing the original lists, and force them to return new list instances.

    (Indeed the observant amongst you will notice that the sort does not need false passed to it, as we already have a new instance thanks to unique)

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

Sidebar

Related Questions

I can't perform copy operations from my DataGrid to the clipboard. I want to
I want to perform some copy actions on the build machine (but not on
So I want to get PHUnit up so I can run selenium server, but
I want to perform a deep copy on an object, does the clone function
I want to run a VBA script from Excel 2007 that can switch focus
I want perform a easy task in android, which when receiving a call the
I want to perform an jquery request to php and then I want php
I want to perform action on change in count of rows of an HTML
I want to perform calculations for each company number in the column PERMNO of
I want to perform a segue, after a assync call to a service. In

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.