Arrays Groovy:与a.intersect(b)和b.intersect(a)的区别

Arrays Groovy:与a.intersect(b)和b.intersect(a)的区别,arrays,groovy,intersect,Arrays,Groovy,Intersect,为什么在Groovy中,当我创建两个列表时,如果我执行a.intersect(b)和b.intersect(a)会有区别 跟踪: Intersect list1 with list2: [world, world, world] Intersect list2 with list1: [world, world] Intersect list1 with list2: [world, world, world, test] Intersect list2 with list1: [world,

为什么在Groovy中,当我创建两个列表时,如果我执行a.intersect(b)和b.intersect(a)会有区别

跟踪:

Intersect list1 with list2: [world, world, world]
Intersect list2 with list1: [world, world]
Intersect list1 with list2: [world, world, world, test]
Intersect list2 with list1: [world, test, test]
(您可以在此处复制它:如果您想测试它)

如果数组都包含唯一的元素,那么它将正常工作。一旦开始添加重复项,就会变得很奇怪:

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 ) );
跟踪:

Intersect list1 with list2: [world, world, world]
Intersect list2 with list1: [world, world]
Intersect list1 with list2: [world, world, world, test]
Intersect list2 with list1: [world, test, test]
我认为
intersect()
的全部目的是为您提供公共元素,所以您将它们放在哪个顺序并不重要

如果不是这样,我如何才能只获取公共元素(除了数组中的重复元素)。例如,示例一应返回
[“世界”、“世界”]
,示例二应返回
[“世界”、“测试”]

编辑

为了澄清一点,这个代码应该测试用户数据仍然是相同的(假设它们在某个中间断开,我们想确保数据没有被篡改,或者处于与以前相同的状态)。 列表的顺序无法保证(用户可以重新排序,但在技术上仍然是“相同的”),并且可以重复

例如:
[“一”、“一”、“二”]
应该匹配
[“二”、“一”、“一”]
,而列表中的任何添加或数据更改都不应该匹配。

如果查看,可以看到该方法的逻辑遵循以下流程:

对于两个集合,

  • 如果
    left
    小于
    right
  • 左侧的所有
    添加到一个集合中(删除重复项)
  • 对于
    right
    中的每个元素,如果它存在于
    leftSet
    中,则将其添加到结果中
  • 最后两个例子

    def array1 = ["hello", "world", "test", "test"]
    def array2 = ["world", "world", "world", "test"]
    
    array1.intersect(array2)
    将给出(如果我们在Groovy中编写相同的算法):

    如果您运行它,您可以看到这意味着结果具有值
    [world,world,world,test]
    (如您所发现的)。这是因为
    right
    中的每个元素都可以在
    leftSet


    不知道为什么第一个示例应该返回
    [“world”,“world”]

    后来。。。 所以,我想你要找的是这样的东西:

    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
    
    为了处理集合中的重复项,因此
    intersect1
    intersect2
    将等于

    [test, world]
    
    后来还是 我相信这正是你想要的:

    [array1,array2]*.groupBy{it}.with { a, b -> assert a == b }
    

    注意:
    defarray1=[“你好”,“世界”,“世界]不在Groovy中创建数组。这是一个
    列表
    (特别是
    数组列表
    )!“不确定为什么第一个示例应该返回
    [“world”,“world”]
    ”-基本上,我正在尝试查看列表是否在两个不同的时间之间发生了更改(服务器代码-我想确保用户返回时具有相同的数据,并且数据没有被篡改)。在第一个示例中,每个列表中出现了两个
    “world”
    ,这就是为什么我要查找类似
    [“world”,“world”]
    的结果的原因。感谢代码示例,但它不能与
    数组1
    之类的内容一起工作,如
    [“hello”,“world”,“world”]
    ,而
    array2
    [“你好”,“你好”,“世界”]-它们都包含重复项,但使用
    TreeSet
    将假定两个列表相同(因为重复项已删除)@divilly ahhh。。。没错。。。你能解释一下你想做什么吗?为什么
    断言[“你好”,“世界”,“世界”!=[“你好”、“你好”、“世界”]
    还不够吗?我不确定交集对你有什么帮助……但是我们的联合力量,我们必须能够想出一个可行的算法……好的,通过以下几点来实现它:1)检查array1和array2的大小。如果他们不一样,那就失败。2)
    def array3=新的ArrayList(array1)
    ,3)
    array2.each{array3.remove(it);}
    4)如果array3的大小在末尾为0,则它们是same@tim_yates对于最后一个,您不能只执行
    断言array1.sort()==array2.sort()
    ?这将更容易理解正在发生的事情!