为什么Groovy List(地图)算法不能按预期工作

时间:2015-11-28 02:17:31

标签: groovy

我试图从地图列表中删除另一个列表中存在的元素。但发现它不能像我预期的那样工作。 我无法理解原因。

def p1 = [1,2,3,4,5]
def p2 = [1,  3,  5]
assert p1[0] == p2[0]
assert p1[2] == p2[1]
assert p1[4] == p2[2]
assert [2,4] == p1-p2 // all ok

def q1 = [[1],[2],[3],[4],[5]]
def q2 = [[1],    [3],    [5]]
assert q1[0] == q2[0]
assert q1[2] == q2[1]
assert q1[4] == q2[2]
assert [[2], [4]] == q1-q2 // all ok

def t1 = [[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]]
def t2 = [[1,2,3],          [7,8,9],             [13,14,15]]
assert t1[0] == t2[0]
assert t1[2] == t2[1]
assert t1[4] == t2[2]
assert [[4,5,6], [10,11,12]] == t1-t2 // all ok

def r1 = [[a:1, b:1], [a:2,b:2], [a:3,b:3],[a:4,b:4], [a:5,b:5]]
def r2 = [[a:1, b:1],            [a:3,b:3],           [a:5,b:5]]
assert r1[0] == r2[0]
assert r1[2] == r2[1]
assert r1[4] == r2[2]
assert [[a:2,b:2], [a:4,b:4]] == r1-r2 // this is what I expected but fails

assert [] == r1-r2 // this is totally unexpected but it's ok

我无法弄清楚原因。有人可以对此有所了解。 感谢

0 个答案:

没有答案