API for set operations in Java? [closed]

2019-01-25 06:03发布

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

标签: java api set
5条回答
倾城 Initia
2楼-- · 2019-01-25 06:09

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.

查看更多
仙女界的扛把子
3楼-- · 2019-01-25 06:19

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.

查看更多
叼着烟拽天下
4楼-- · 2019-01-25 06:21

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();
查看更多
甜甜的少女心
5楼-- · 2019-01-25 06:23

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

查看更多
在下西门庆
6楼-- · 2019-01-25 06:24

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

查看更多
登录 后发表回答