Java 中集合操作的 API?

发布于 2024-11-05 02:47:51 字数 1539 浏览 1 评论 0原文

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

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

发布评论

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

评论(5

自由如风 2024-11-12 02:47:51

是的,java Set 类。

通过 Java SE 教程:

s1.containsAll(s2) — 如果 s2 是 s1 的子集,则返回 true。 (s2 是
如果集合 s1 包含 s2 中的所有元素,则为 s1 的子集。)

s1.addAll(s2) — 将 s1 转换为 s1 和 s2 的并集。 (这
两个集合的并集是包含所有包含的元素的集合
在任一组中。)

s1.retainAll(s2) — 将 s1 转换为 s1 和 s2 的交集。
(两个集合的交集是仅包含元素的集合
两组通用。)

s1.removeAll(s2) — 将 s1 转换为(非对称)集合
s1 和 s2 的差异。 (例如,s1 的设定差值减去
s2 是包含在 s1 中找到但不在 s1 中找到的所有元素的集合
s2.)

http://download.oracle.com/javase/tutorial/集合/接口/set.html

Yes, the java Set class.

Via Java SE tutorial:

s1.containsAll(s2) — returns true if s2 is a subset of s1. (s2 is a
subset of s1 if set s1 contains all of the elements in s2.)

s1.addAll(s2) — transforms s1 into the union of s1 and s2. (The
union of two sets is the set containing all of the elements contained
in either set.)

s1.retainAll(s2) — transforms s1 into the intersection of s1 and s2.
(The intersection of two sets is the set containing only the elements
common to both sets.)

s1.removeAll(s2) — transforms s1 into the (asymmetric) set
difference of s1 and s2. (For example, the set difference of s1 minus
s2 is the set containing all of the elements found in s1 but not in
s2.)

http://download.oracle.com/javase/tutorial/collections/interfaces/set.html

一花一树开 2024-11-12 02:47:51

我不知道任何 API,但已使用以下方法在 设置

public static <T> Set<T> union(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    tmp.addAll(setB);
    return tmp;
  }

  public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>();
    for (T x : setA)
      if (setB.contains(x))
        tmp.add(x);
    return tmp;
  }

  public static <T> Set<T> difference(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    tmp.removeAll(setB);
    return tmp;
  }

  public static <T> Set<T> symDifference(Set<T> setA, Set<T> setB) {
    Set<T> tmpA;
    Set<T> tmpB;

    tmpA = union(setA, setB);
    tmpB = intersection(setA, setB);
    return difference(tmpA, tmpB);
  }

  public static <T> boolean isSubset(Set<T> setA, Set<T> setB) {
    return setB.containsAll(setA);
  }

  public static <T> boolean isSuperset(Set<T> setA, Set<T> setB) {
    return setA.containsAll(setB);
  }

参考:集合操作:并集、交集、差集、对称差集、is子集,是超集

I don't know any API but have used following methods do such things on Set.

public static <T> Set<T> union(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    tmp.addAll(setB);
    return tmp;
  }

  public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>();
    for (T x : setA)
      if (setB.contains(x))
        tmp.add(x);
    return tmp;
  }

  public static <T> Set<T> difference(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    tmp.removeAll(setB);
    return tmp;
  }

  public static <T> Set<T> symDifference(Set<T> setA, Set<T> setB) {
    Set<T> tmpA;
    Set<T> tmpB;

    tmpA = union(setA, setB);
    tmpB = intersection(setA, setB);
    return difference(tmpA, tmpB);
  }

  public static <T> boolean isSubset(Set<T> setA, Set<T> setB) {
    return setB.containsAll(setA);
  }

  public static <T> boolean isSuperset(Set<T> setA, Set<T> setB) {
    return setA.containsAll(setB);
  }

Reference: Set operations: union, intersection, difference, symmetric difference, is subset, is superset

忆沫 2024-11-12 02:47:51

Google Guava 库还有很多有用的方法(例如集合并集和差集)。

https://code.google.com/p/guava-libraries/wiki /CollectionUtilitiesExplained#Sets

示例(来自上面链接的页面):

Set<String> wordsWithPrimeLength = ImmutableSet.of("one", "two", "three", "six", "seven", "eight");
Set<String> primes = ImmutableSet.of("two", "three", "five", "seven");

SetView<String> intersection = Sets.intersection(primes, wordsWithPrimeLength); // contains "two", "three", "seven"
// I can use intersection as a Set directly, but copying it can be more efficient if I use it a lot.
return intersection.immutableCopy();

The Google Guava library also has a bunch of useful methods (e.g. set union and difference).

https://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained#Sets

Example (from page linked above):

Set<String> wordsWithPrimeLength = ImmutableSet.of("one", "two", "three", "six", "seven", "eight");
Set<String> primes = ImmutableSet.of("two", "three", "five", "seven");

SetView<String> intersection = Sets.intersection(primes, wordsWithPrimeLength); // contains "two", "three", "seven"
// I can use intersection as a Set directly, but copying it can be more efficient if I use it a lot.
return intersection.immutableCopy();
我喜欢麦丽素 2024-11-12 02:47:51

java.util.Set 类没有这些调用在其 API 中,但您可以组合 removeAll()retainAll()addAll() 等操作来进行并集、交集,和差异。我不确定我知道您所说的域限制是什么意思。

The java.util.Set class doesn't have those calls in its API, but you can combine operations like removeAll(), retainAll(), and addAll() to do union, intersection, and difference. I'm not sure I know what you mean by domain restriction.

转身泪倾城 2024-11-12 02:47:51

从 API 设置

您可以 '使用接受任何 Collection 作为输入参数的 keepAll、removeAll 和 addAll 方法模拟交集、差异、域限制。

Set from the API

You can 'simulate' intersection, difference, domain restriction with retainAll, removeAll and addAll method that accept any Collection as a input parameter.

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