Advertisement
scottashipp

HashSet illustration

May 2nd, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.util.HashSet;
  2.  
  3. public class testCollections {
  4.    
  5.     /*
  6.      * testObj is just a wrapper for an int and will be used in an experiment to test hashing and so on
  7.      */
  8.     private class testObj {
  9.        
  10.         private int value = 0;
  11.        
  12.         public testObj(int newValue) {
  13.             value = newValue;
  14.         }
  15.        
  16.         public int getValue() {
  17.             return value;
  18.         }
  19.        
  20.         public boolean equals(Object o) {
  21.            
  22.             if(!(o instanceof testObj)) {
  23.                 return false;
  24.             }
  25.            
  26.             if(o == this) {
  27.                 return true;
  28.             }
  29.             testObj newO = (testObj) o;
  30.            
  31.             if(newO.getValue() == value) {
  32.                 return true;
  33.             }
  34.            
  35.             return false;
  36.         }
  37.        
  38.         public int hashCode() {
  39.             return value;
  40.         }
  41.  
  42.     }
  43.    
  44.     public static void main(String[] args) {
  45.         HashSet<testObj> mySet = new HashSet<testObj>();
  46.         testObj temp = new testCollections().new testObj(5);
  47.         mySet.add(temp);
  48.         System.out.print("The set now ");
  49.         System.out.print( (mySet.contains(temp)) ?  "contains " : "does not contain ");
  50.         System.out.println("object with value " + temp.getValue() + " and reference " + temp.toString());
  51.         System.out.println("Adding another of the same object.");
  52.         if(mySet.add(temp)) {
  53.             System.out.println("Add successful.");
  54.         }
  55.         else {
  56.             System.out.println("Add unsuccessful.");
  57.         }
  58.         System.out.println("Are there two of the same object now? Will try to remove one and then see if one is still in there.");
  59.         if(mySet.remove(temp)) {
  60.             System.out.println("Removed it.");
  61.         }
  62.         else {
  63.              System.out.println("Couldn't remove it because it wasn't there.");
  64.         }
  65.         System.out.print("The set now ");
  66.         System.out.print( (mySet.contains(temp)) ? "still contains " : " does not contain ");
  67.         System.out.println("object with value " + temp.getValue() + " and reference " + temp.toString());
  68.         if(mySet.remove(temp)) {
  69.             System.out.println("Removed it.");
  70.         }
  71.         else {
  72.              System.out.println("Couldn't remove it because it wasn't there.");
  73.         }
  74.        
  75.         System.out.println();
  76.         System.out.println("------------------");
  77.         System.out.println();
  78.         testObj temp2 = new testCollections().new testObj(5);
  79.         System.out.println("The HashSet is currently empty? " + mySet.isEmpty());
  80.         System.out.println("Have created a second object with the same hashcode.");
  81.         System.out.println("The two objects are equal? " + temp.equals(temp2));
  82.         System.out.println("temp.hashCode is " + temp.hashCode() + " and temp2.hashCode is " + temp2.hashCode());
  83.         System.out.println("Adding temp to the HashSet.");
  84.         mySet.add(temp);
  85.         System.out.println("The HashSet contains temp? " + mySet.contains(temp));
  86.         System.out.println("The HashSet contains temp2? " + mySet.contains(temp2));
  87.         System.out.println("That is true because their hashCode and .equals values return the same thing.");
  88.         System.out.println("Now attempting to add temp2 to the HashSet.");
  89.         if(mySet.add(temp2)) {
  90.             System.out.println("Successfully added temp2");
  91.         }
  92.         else {
  93.             System.out.println("Could not add temp2 because value already exists.");
  94.         }
  95.        
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement