Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- public class testCollections {
- /*
- * testObj is just a wrapper for an int and will be used in an experiment to test hashing and so on
- */
- private class testObj {
- private int value = 0;
- public testObj(int newValue) {
- value = newValue;
- }
- public int getValue() {
- return value;
- }
- public boolean equals(Object o) {
- if(!(o instanceof testObj)) {
- return false;
- }
- if(o == this) {
- return true;
- }
- testObj newO = (testObj) o;
- if(newO.getValue() == value) {
- return true;
- }
- return false;
- }
- public int hashCode() {
- return value;
- }
- }
- public static void main(String[] args) {
- HashSet<testObj> mySet = new HashSet<testObj>();
- testObj temp = new testCollections().new testObj(5);
- mySet.add(temp);
- System.out.print("The set now ");
- System.out.print( (mySet.contains(temp)) ? "contains " : "does not contain ");
- System.out.println("object with value " + temp.getValue() + " and reference " + temp.toString());
- System.out.println("Adding another of the same object.");
- if(mySet.add(temp)) {
- System.out.println("Add successful.");
- }
- else {
- System.out.println("Add unsuccessful.");
- }
- 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.");
- if(mySet.remove(temp)) {
- System.out.println("Removed it.");
- }
- else {
- System.out.println("Couldn't remove it because it wasn't there.");
- }
- System.out.print("The set now ");
- System.out.print( (mySet.contains(temp)) ? "still contains " : " does not contain ");
- System.out.println("object with value " + temp.getValue() + " and reference " + temp.toString());
- if(mySet.remove(temp)) {
- System.out.println("Removed it.");
- }
- else {
- System.out.println("Couldn't remove it because it wasn't there.");
- }
- System.out.println();
- System.out.println("------------------");
- System.out.println();
- testObj temp2 = new testCollections().new testObj(5);
- System.out.println("The HashSet is currently empty? " + mySet.isEmpty());
- System.out.println("Have created a second object with the same hashcode.");
- System.out.println("The two objects are equal? " + temp.equals(temp2));
- System.out.println("temp.hashCode is " + temp.hashCode() + " and temp2.hashCode is " + temp2.hashCode());
- System.out.println("Adding temp to the HashSet.");
- mySet.add(temp);
- System.out.println("The HashSet contains temp? " + mySet.contains(temp));
- System.out.println("The HashSet contains temp2? " + mySet.contains(temp2));
- System.out.println("That is true because their hashCode and .equals values return the same thing.");
- System.out.println("Now attempting to add temp2 to the HashSet.");
- if(mySet.add(temp2)) {
- System.out.println("Successfully added temp2");
- }
- else {
- System.out.println("Could not add temp2 because value already exists.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement