Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class MySet extends HashSet<String> {
- private static final long serialVersionUID = 1L;
- public MySet() {
- super();
- }
- /**
- * @return the union of the elements of this and that
- */
- public MySet union(MySet that) {
- MySet result = new MySet();
- result.addAll(this);
- if (that == null || that.size() < 1) {
- return result;
- }
- result.addAll(that);
- return result;
- }
- /**
- * @return the intersection of the elements of this and that
- */
- public MySet intersection(MySet that) {
- MySet result = new MySet();
- if (that == null || that.size() < 1) {
- return result;
- }
- for (String s : this) {
- if (that.contains(s)) {
- result.add(s);
- }
- }
- return result;
- }
- /**
- * @return the difference of the elements of this and that
- */
- public MySet difference(MySet that) {
- MySet result = new MySet();
- if (that == null || that.size() < 1) {
- result.addAll(this);
- return result;
- }
- for(String s : this) {
- if (!that.contains(s)) {
- result.add(s);
- }
- }
- return result;
- }
- /**
- * @return the exclusive or (XOR) of the elements of this and that
- */
- public MySet exclusiveOr(MySet that) {
- MySet result = new MySet();
- if (that == null || that.size() < 1) {
- result.addAll(this);
- return result;
- }
- MySet intersection = this.intersection(that);
- MySet union = this.union(that);
- for (String s : union) {
- if(!intersection.contains(s)) {
- result.add(s);
- }
- }
- return result;
- }
- /**
- * @return a String representation of a MySet object
- */
- public String toString() {
- String res = "<MySet{";
- for (String s : this) {
- res += s + ",";
- }
- res = res.substring(0, res.length() - 1);
- res += "}>";
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement