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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:27:54+00:00 2026-05-25T13:27:54+00:00

Why in Groovy, when I create 2 lists, is there a difference if I

  • 0

Why in Groovy, when I create 2 lists, is there a difference if I do a.intersect( b ) and b.intersect( a ):

def list1 = ["hello", "world", "world"];
def list2 = ["world", "world", "world"];

println( "Intersect list1 with list2: " + list1.intersect( list2 ) );
println( "Intersect list2 with list1: " + list2.intersect( list1) );

traces:

Intersect list1 with list2: [world, world, world]
Intersect list2 with list1: [world, world]

(you can copy it here: http://groovyconsole.appspot.com/ if you want to test it)

If the arrays all contain unique elements, then it works as normal. Once you begin to add duplicates, it gets weird:

def list1 = ["hello", "world", "test", "test"];
def list2 = ["world", "world", "world", "test"];

println( "Intersect list1 with list2: " + list1.intersect( list2 ) );
println( "Intersect list2 with list1: " + list2.intersect( list1 ) );

traces:

Intersect list1 with list2: [world, world, world, test]
Intersect list2 with list1: [world, test, test]

I thought the whole point of intersect() was to give you the common elements, so it didn’t matter which order you put them in?

If this isn’t the case, how can I get only the common elements (expect duplicates in the array). E.g. example one should return ["world", "world"] and example two should return ["world", "test"]

Edit

To clarify a bit, this code should test that user data is still the same (assuming they disconnected in the middle of something and we want to make sure the data hasn’t been tampered with, or is in the same state as before).

The order of the lists can’t be guaranteed (the user could reorder it, but it’s still technically the “same”), and duplicates are possible.

So something like: ["one", "one", "two"] should match ["two", "one", "one"], whereas any addition to the lists, or change in data shouldn’t match.

  • 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-25T13:27:54+00:00Added an answer on May 25, 2026 at 1:27 pm

    If you look at the source for Collection.intersect, you can see that the logic of the method follows this flow:

    for two collections, left and right

    1. Swap left and right if left is smaller than right
    2. Add all of left into a Set (removes duplicates)
    3. For each element in right if it exists in the leftSet, then add it to the results

    So, for your last 2 examples;

    def array1 = ["hello", "world", "test", "test"]
    def array2 = ["world", "world", "world", "test"]
    

    array1.intersect( array2 ) would give (if we wrote that same algorithm in Groovy):

    leftSet = new TreeSet( array1 ) // both same size, so no swap
    // leftSet = [ 'hello', 'world', 'test' ]
    right   = array2
    result = right.findAll { e -> leftSet.contains( e ) }
    

    Which (if you run it), you can see means result has the value [world, world, world, test] (as you found). This is because every element in right can be found in the leftSet

    Not sure why the first example should return ["world","world"] though…

    later…

    So, what I think you are looking for would be something like this:

    def array1 = ["hello", "world", "test", "test"]
    def array2 = ["world", "world", "world", "test"]
    def intersect1 = array1.intersect( array2 ) as TreeSet
    def intersect2 = array2.intersect( array1 ) as TreeSet
    assert intersect1 == intersect2
    

    in order that you cope with the duplicates in the collections, as then both intersect1 and intersect2 will be equal to

    [test, world]
    

    later still

    I believe this does what you want:

    [array1,array2]*.groupBy{it}.with { a, b -> assert a == b }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Groovy adds the execute method to String to make executing shells fairly easy; println
I am a total Groovy newbie. I saw the following code here . def
I am trying to create a multi line string in Groovy. I have a
I'm using groovy.xml.MarkupBuilder to create XML response but it creates prettyprinted result which is
I'm trying to use Groovy to create an interactive scripting / macro mode for
I am attempting to create a script that wraps a Groovy class that will
Using Groovy's package name convention, I can intercept Groovy method calls to a Java
I am using groovy to create some mock classes for a test case. I
I wanted to create a web application. I thought of selecting Groovy or Grails.
Can anyone tell me how to create multiple records in grails. This class is

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.