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();
- for (String s : this)
- result.add(s);
- if (that != null)
- for (String s : that)
- result.add(s);
- return result;
- }
- /**
- * @return the intersection of the elements of this and that
- */
- public MySet intersection(MySet that)
- {
- MySet result = new MySet();
- if (that != null)
- 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)
- for (String s : this)
- {
- if(!that.contains(s))
- result.add(s);
- }
- else
- for (String s : this)
- 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)
- {
- for (String s : this)
- if(!that.contains(s))
- result.add(s);
- for (String s : that)
- if(!this.contains(s))
- result.add(s);
- }
- else
- for (String s : this)
- result.add(s);
- return result;
- }
- /**
- * @return a String representation of a MySet object
- */
- public String toString()
- {
- String result = "<MySet{";
- for (String s : this)
- result += (s + ",");
- result = result.substring(0, result.length()-1);
- return result + "}> ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement