API for set operations in Java? [closed]

牧云@^-^@ 提交于 2019-12-18 04:34:21

问题


Is there an API for Set operations like Union, intersection, difference, Cartesian product, Function from a set to another, domain restriction and range restriction of those functions, .... in Java?

Please comment on coverage (of operations) and performance.

Thanks


回答1:


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




回答2:


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




回答3:


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();



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/5868808/api-for-set-operations-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!