与常规集合中的交集相反

发布于 2024-11-01 22:16:13 字数 23 浏览 4 评论 0原文

在常规集合中相交的反义词是什么?

what would be the opposite of intersect in groovy collections?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

穿越时光隧道 2024-11-08 22:16:13

你可能想结合@Andre和@denis的答案

我认为你想要的是并集,然后从中减去交集

def a = [1,2,3,4,5]
def b = [2,3,4]

assert [1,5] == ( a + b ) - a.intersect( b )

Denis给出的解决方案将取决于你是否愿意

def opposite = leftCollection-rightCollection // [1,5]

def opposite = rightCollection-leftCollection // []

我认为你不想要

You probably want to combine both the answers from @Andre and @denis

I think what you want is the union and then subtract the intersection from this

def a = [1,2,3,4,5]
def b = [2,3,4]

assert [1,5] == ( a + b ) - a.intersect( b )

The solution given by denis would depend on whether you do

def opposite = leftCollection-rightCollection // [1,5]

or

def opposite = rightCollection-leftCollection // []

which I don't think you wanted

Spring初心 2024-11-08 22:16:13

我不确定你所说的“并集的相反”是什么意思,但我的猜测是你的意思是对称差异(又名集合差异或析取)。该操作的结果如下图红色所示。

在此处输入图像描述

在两个 Java/Groovy 集合上执行此操作的最简单方法是使用 析取 方法由 Apache commons 集合提供。

I'm not certain what you mean by "opposite of union", but my guess is that you mean symmetric difference (AKA set difference or disjunction). The result of this operation is shown in red below.

enter image description here

The easiest way to perform this operation on two Java/Groovy collections is to use the disjunction method provided by Apache commons collections.

拥抱影子 2024-11-08 22:16:13

会是这个吗?

def leftCollection = [1,2,3,4,5]
def rightCollection = [2,3,4]
def opposite = leftCollection-rightCollection
println opposite

印刷

[1,5]

Could it be this?

def leftCollection = [1,2,3,4,5]
def rightCollection = [2,3,4]
def opposite = leftCollection-rightCollection
println opposite

Prints

[1,5]
流星番茄 2024-11-08 22:16:13

使用 intersect 表示相交,

assert [4,5] == [1,2,3,4,5].intersect([4,5,6,7,8])

使用 + 表示并集:

assert [1,2,3,4,5] == [1,2,3] + [4,5]

请参阅 http://groovy.codehaus .org/groovy-jdk/java/util/Collection.html

use intersect for intersections

assert [4,5] == [1,2,3,4,5].intersect([4,5,6,7,8])

use + for unions:

assert [1,2,3,4,5] == [1,2,3] + [4,5]

see http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html

情深如许 2024-11-08 22:16:13
(a-b)+(b-a)
// (a-b) return [1,5]
//(b-a) return []
// TOTAL = [1,5]+[]

这是当我们有: a=[1,2,3,4,5],b=[2,3,4,5]

OOP :

java.util.List.metaClass.oppIntersect={b->
  return ((delegate-b)+(b-delegate))
}

然后

a.oppIntersect(b)

结束!

(a-b)+(b-a)
// (a-b) return [1,5]
//(b-a) return []
// TOTAL = [1,5]+[]

this is when we have: a=[1,2,3,4,5],b=[2,3,4,5]

OOP :

java.util.List.metaClass.oppIntersect={b->
  return ((delegate-b)+(b-delegate))
}

then

a.oppIntersect(b)

END!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文