Advertisement
Guest User

7.1

a guest
Jan 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. class MySet extends HashSet<String>
  5. {
  6.   private static final long serialVersionUID = 1L;
  7.  
  8.   public MySet()
  9.   {
  10.     super();
  11.   }
  12.  
  13.   /**
  14.    * @return the union of the elements of this and that
  15.    */
  16.   public MySet union(MySet that)
  17.   {
  18.     MySet result = new MySet();
  19.  
  20.     for (String s : this)
  21.       result.add(s);
  22.    
  23.     if (that != null)
  24.       for (String s : that)
  25.         result.add(s);            
  26.  
  27.     return result;
  28.   }
  29.  
  30.   /**
  31.    * @return the intersection of the elements of this and that
  32.    */
  33.   public MySet intersection(MySet that)
  34.   {
  35.     MySet result = new MySet();
  36.    
  37.     if (that != null)
  38.       for (String s : this)
  39.         if(that.contains(s))
  40.           result.add(s);
  41.  
  42.     return result;
  43.   }
  44.  
  45.   /**
  46.    * @return the difference of the elements of this and that
  47.    */
  48.   public MySet difference(MySet that)
  49.   {
  50.     MySet result = new MySet();
  51.    
  52.     if (that != null)
  53.       for (String s : this)
  54.       {
  55.         if(!that.contains(s))
  56.           result.add(s);
  57.       }
  58.          
  59.     else
  60.       for (String s : this)
  61.         result.add(s);
  62.  
  63.     return result;
  64.   }
  65.  
  66.   /**
  67.    * @return the exclusive or (XOR) of the elements of this and that
  68.    */
  69.   public MySet exclusiveOr(MySet that)
  70.   {
  71.     MySet result = new MySet();
  72.    
  73.     if (that != null)
  74.     {
  75.       for (String s : this)
  76.         if(!that.contains(s))
  77.           result.add(s);  
  78.      
  79.       for (String s : that)
  80.         if(!this.contains(s))
  81.           result.add(s);
  82.     }
  83.    
  84.     else
  85.       for (String s : this)
  86.         result.add(s);
  87.  
  88.     return result;
  89.   }
  90.  
  91.   /**
  92.    * @return a String representation of a MySet object
  93.    */
  94.   public String toString()
  95.   {
  96.     String result = "<MySet{";
  97.  
  98.     for (String s : this)
  99.       result += (s + ",");
  100.  
  101.     result = result.substring(0, result.length()-1);
  102.     return result + "}> ";
  103.   }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement