Java:是否有一种简单、快速的方法可以将 AND、OR 或 XOR 组合在一起?

发布于 2024-07-11 23:15:21 字数 227 浏览 4 评论 0原文

也就是说,如果我有两个或更多集合,并且我想返回一个新集合,其中包含以下任一集合:

  1. 每个集合共有的所有元素 (AND)。
  2. 每组的所有元素总计 (OR)。
  3. 每组的所有元素都是独一无二的。 (异或)。

有没有一种简单的、预先存在的方法可以做到这一点?

编辑:这是错误的术语,不是吗?

That is, if I had two or more sets, and I wanted to return a new set containing either:

  1. All of the elements each set has in common (AND).
  2. All of the elements total of each set (OR).
  3. All of the elements unique to each set. (XOR).

Is there an easy, pre-existing way to do that?

Edit: That's the wrong terminology, isn't it?

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

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

发布评论

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

评论(5

落花随流水 2024-07-18 23:15:21

假设 2 集合对象 a 和 b

AND(两个集合的交集)

a.retainAll(b); 

OR(两个集合的并集)

a.addAll(b);

XOR
要么滚动你自己的循环:

foreach item
if(a.contains(item) and !b.contains(item) ||  (!a.contains(item) and b.contains(item)))
 c.add(item)

要么执行以下操作:

c.addAll(a); 
c.addAll(b);
a.retainAll(b); //a now has the intersection of a and b
c.removeAll(a); 

请参阅 Set文档 和此页面。 欲了解更多。

Assuming 2 Set objects a and b

AND(intersection of two sets)

a.retainAll(b); 

OR(union of two sets)

a.addAll(b);

XOR
either roll your own loop:

foreach item
if(a.contains(item) and !b.contains(item) ||  (!a.contains(item) and b.contains(item)))
 c.add(item)

or do this:

c.addAll(a); 
c.addAll(b);
a.retainAll(b); //a now has the intersection of a and b
c.removeAll(a); 

See the Set documentation and this page. For more.

累赘 2024-07-18 23:15:21

您可以使用 Google- Collections Sets 类 具有交集()、联合() 和对称差值() 方法。

Sets.intersection(set1, set2);
Sets.union(set1, set2);

SetView view = Sets.intersection(Sets.union(set1, set2), set3);
Set result = view.copyInto(new HashSet());

You can use the Google-Collections Sets class which has the methods intersection() union() and symmetricDifference().

Sets.intersection(set1, set2);
Sets.union(set1, set2);

SetView view = Sets.intersection(Sets.union(set1, set2), set3);
Set result = view.copyInto(new HashSet());
游魂 2024-07-18 23:15:21

@米尔豪斯 说:

查看集合 API。 如果你使用
添加您可以获得的所有内容或。 如果你使用
保留所有你能得到的和。 我不
了解异或。

看起来如果你有集合 s1s2 你可以这样做来获得异或:

  1. 将集合 s1 复制到 s3
  2. s1.removeAll(s2); (s1 现在包含 s2 中不存在的所有元素)
  3. s2.removeAll(s3); (s2 现在包含 s3 中不存在的所有元素 =旧的 s1)
  4. s1.addAll(s2); (s1 现在包含上述两个集合的并集)

@Milhous said:

check out the sets api. if you use
addAll you can get or. If you use
retainAll you can get the and. I dont
know about the Xor.

It seems like if you had sets s1 and s2 you could do this to get XOR:

  1. copy the set s1 to s3
  2. s1.removeAll(s2); (s1 now contains all elements not in s2)
  3. s2.removeAll(s3); (s2 now contains all elements not in s3 = the old s1)
  4. s1.addAll(s2); (s1 now contains the union of the above two sets)
晌融 2024-07-18 23:15:21

查看集合 api。 如果你使用 addAll 你可以得到 or 。 如果您使用retainAll,您可以获得and。 我不知道异或。

编辑:来自设置的文档。

...如果指定的集合也是一个集合,则 addAll 操作会有效地修改此集合,使其值是两个集合的并集。

....如果指定的集合也是一个集合,则此操作有效地修改该集合,使其值是两个集合的交集。

check out the sets api. if you use addAll you can get or. If you use retainAll you can get the and. I dont know about the Xor.

Edit: from the set documentation.

...If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets.

....If the specified collection is also a set, this operation effectively modifies this set so that its value is the intersection of the two sets.

面犯桃花 2024-07-18 23:15:21

我非常确定 Jakarta Common Collections API 支持并集、交集等。

如果 Google Collections API 不支持,我会感到惊讶。

I'm pretty sure that Jakarta Common Collections API supports unions, intersections etc.

I'd be amazed if the Google Collections API didn't as well.

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