I tried adding the iterator in the Union method but no values were presented. Instead it provided the HashSet@1546 Memory Location address. We are not allowed to use import java.util.Set or import java.util.HashSet or import.util.Collections How would obtain the values using the iterator in the Union method?
Below is the HashSetTester code,
import java.util.Iterator;
public class HashSetTester {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
HashSet Set_A = new HashSet(101);
Set_A.add(1);
Set_A.add(2);
Set_A.add(3);
Set_A.add(4);
Set_A.add(5);
Set_A.add(6);
Set_A.add(11);
Set_A.remove(9);
HashSet Bnames = new HashSet(101);
Bnames.add(1);
Bnames.add(2);
Bnames.add(0);
Bnames.add(8);
Bnames.add(9);
Bnames.add(10);
Bnames.remove(13);
Iterator Aiter = Set_A.iterator();
System.out.print("Set A: ");
while(Aiter.hasNext())
{
System.out.print(Aiter.next() + " ");
}
Iterator Biter = Bnames.iterator();
System.out.print("\nSet B: ");
while(Biter.hasNext())
{
System.out.print(Biter.next() + " ");
}
System.out.println();
HashSet union = new HashSet(101);
union.getUnion(Set_A, Bnames);
HashSet intersect = new HashSet(101);
intersect.getIntersect(Set_A, Bnames);
HashSet difference = new HashSet(101);
difference.getDifference(Set_A, Bnames);
}
}
And then then the given HashSet Table.
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
This class implements a hash set using separate chaining.
*/
public class HashSet
{
private Node[] buckets;
private int currentSize;
/**
Constructs a hash table.
@param bucketsLength the length of the buckets array
*/
public HashSet(int bucketsLength)
{
buckets = new Node[bucketsLength];
currentSize = 0;
}
/**
Tests for set membership.
@param x an object
@return true if x is an element of this set
*/
public boolean contains(Object x)
{
int h = x.hashCode();
if (h < 0) { h = -h; }
h = h % buckets.length;
Node current = buckets[h];
while (current != null)
{
if (current.data.equals(x)) { return true; }
current = current.next;
}
return false;
}
/**
Adds an element to this set.
@param x an object
@return true if x is a new object, false if x was
already in the set
*/
public boolean add(Object x)
{
int h = x.hashCode();
if (h < 0) { h = -h; }
h = h % buckets.length;
Node current = buckets[h];
while (current != null)
{
if (current.data.equals(x)) { return false; }
// Already in the set
current = current.next;
}
Node newNode = new Node();
newNode.data = x;
newNode.next = buckets[h];
buckets[h] = newNode;
currentSize++;
return true;
}
/**
Removes an object from this set.
@param x an object
@return true if x was removed from this set, false
if x was not an element of this set
*/
public boolean remove(Object x)
{
int h = x.hashCode();
if (h < 0) { h = -h; }
h = h % buckets.length;
Node current = buckets[h];
Node previous = null;
while (current != null)
{
if (current.data.equals(x))
{
if (previous == null) { buckets[h] = current.next; }
else { previous.next = current.next; }
currentSize--;
return true;
}
previous = current;
current = current.next;
}
return false;
}
/**
Returns an iterator that traverses the elements of this set.
@return a hash set iterator
*/
public Iterator iterator()
{
return new HashSetIterator();
}
/**
Gets the number of elements in this set.
@return the number of elements
*/
public int size()
{
return currentSize;
}
class Node
{
public Object data;
public Node next;
}
class HashSetIterator implements Iterator
{
private int bucketIndex;
private Node current;
/**
Constructs a hash set iterator that points to the
first element of the hash set.
*/
public HashSetIterator()
{
current = null;
bucketIndex = -1;
}
public boolean hasNext()
{
if (current != null && current.next != null) { return true; }
for (int b = bucketIndex + 1; b < buckets.length; b++)
{
if (buckets[b] != null) { return true; }
}
return false;
}
public Object next()
{
if (current != null && current.next != null)
{
current = current.next; // Move to next element in bucket
}
else // Move to next bucket
{
do
{
bucketIndex++;
if (bucketIndex == buckets.length)
{
throw new NoSuchElementException();
}
current = buckets[bucketIndex];
}
while (current == null);
}
return current.data;
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
public void getUnion(HashSet s1, HashSet s2)
{
HashSet tempUnion = new HashSet(101);
for (int i = 0; i < s1.size(); i++)
tempUnion.add(s1.add(i));
for (int j = 0; j < s2.size(); j++)
tempUnion.add(s2.add(j));
System.out.print("\nThe Union Set is: " + tempUnion);
}
/**
* intersect() method returns a set which is the result of the
* intersection of the two sets
* @param s1
* @param s2
*/
public void getIntersect(HashSet s1, HashSet s2)
{
HashSet intersect = new HashSet(101);
int i = 0;
int j = 0;
while(i < s1.size() && j < s2.size()){
add(s1);
add(s2);
if(s1.contains(s2) == s2.contains(s1))
{
intersect.add(i);
intersect.add(j);
i++;
j++;
}
}
System.out.print("\nThe Intersection Set is: " + intersect);
}
//A & ~B = A - B
public void getDifference(HashSet s1, HashSet s2)
{
HashSet difference = new HashSet(101);
difference.add(s1);
if(s1.contains(s2))
s2.remove(s1);
System.out.println("\nThe Difference Set is: " + difference);
}
public void toString(HashSet s1, HashSet s2)
{
HashSet Union = new HashSet(101);
HashSet Inter = new HashSet(101);
HashSet Diffe = new HashSet(101);
Union.getUnion(s1, s2);
Inter.getIntersect(s1, s2);
Diffe.getDifference(s1, s2);
}
}