use of a hash table is to implement a set data type. You will implement the methods addElement, find, toString, resize, and the MySetIterator inner class in the class MySet. MySet uses a separate chaining hash table to implement a set of integers. To get started, import the starter file, MySet.java into the hashset package you create in a new Java Project. Please do not change any of the method signatures in either class. Implement the methods described below. You are free to test your code however you prefer. Below is method Signature class: package hashset; import java.util.Iterator; public class MySet { // implements a set using a separate chaining hash table private class Node { private Integer element; private Node next; private Node(Integer e, Node n) { element = e; next = n; } } private Node table[]; //an array of linked list private int tableSize; //current number of lists in the table private int numElements; //number of elements in the set private final int primes[] = {7, 23, 59, 131, 271, 563, 1171, 2083, 4441, 8839, 16319, 32467, 65701, 131413, 263983, 528991}; private int primeIndex; //last prime used private int nextPrime(int p) { //finds the next prime from the list above //used for resizing and the initial size while (primes[primeIndex] <= p) primeIndex++; return primes[primeIndex]; } public MySet(int s) { //s is a hint for the initial size primeIndex = 0; tableSize = nextPrime(s); table = new Node[tableSize]; numElements = 0; } //return the hash function value for k private int hash(Integer k) { return Math.abs(k.hashCode() % tableSize); } //"double" the table size and reinsert the values stored in the //current table. the table size should remain prime private void resize() { } //returns true when e is in the set, otherwise returns false public boolean find(Integer e) { return false; } //if e is not in the set add e to the set otherwise the set does not change //if after adding the new element numElements > 2*tableSize then call resize public void addElement(Integer e) { } //returns a string representation for the set //the string representation of the set is { followed by a comma delimiter list of set //elements followed by a }. The string for the empty set is {} //For example, {1,2,3}. //Note that you SHOULD NOT have any spaces in your String /* * Example: * table[0]: 2 -> 4 * table[1]: 1 -> 3 * * The string representation of this set is {2,4,1,3} */ public String toString() { } public class MySetIterator implements Iterator { //implements an iterator for the set private int currentList; private Node currentNode; //helper method that finds the next non empty bucket private void nextList() { while (currentList < tableSize && table[currentList] == null) { currentList++; } currentNode = (currentList < tableSize) ? table[currentList] : null; } public MySetIterator() { currentList = 0; nextList(); } //returns true if the iteration has more elements. public boolean hasNext() { return currentNode != null; } //Returns the next element in the iteration. public Integer next() { Integer rVal = currentNode.element; if (currentNode.next != null) { //what should the currentNode be? } else { //No more elements in the current bucket //I need to get the next bucket } return rVal; } } public Iterator iterator() { //returns an iterator for the set return new MySetIterator
One use of a hash table is to implement a set data type. You will implement the methods addElement, find, toString, resize, and the MySetIterator inner class in the class MySet. MySet uses a separate chaining hash table to implement a set of integers. To get started, import the starter file, MySet.java into the hashset package you create in a new Java Project. Please do not change any of the method signatures in either class. Implement the methods described below. You are free to test your code however you prefer.
Below is method Signature class:
package hashset;
import java.util.Iterator;
public class MySet {
// implements a set using a separate chaining hash table
private class Node {
private Integer element;
private Node next;
private Node(Integer e, Node n) {
element = e;
next = n;
}
}
private Node table[]; //an array of linked list
private int tableSize; //current number of lists in the table
private int numElements; //number of elements in the set
private final int primes[] = {7, 23, 59, 131, 271, 563, 1171,
2083, 4441, 8839, 16319, 32467,
65701, 131413, 263983, 528991};
private int primeIndex; //last prime used
private int nextPrime(int p) {
//finds the next prime from the list above
//used for resizing and the initial size
while (primes[primeIndex] <= p)
primeIndex++;
return primes[primeIndex];
}
public MySet(int s) {
//s is a hint for the initial size
primeIndex = 0;
tableSize = nextPrime(s);
table = new Node[tableSize];
numElements = 0;
}
//return the hash function value for k
private int hash(Integer k) {
return Math.abs(k.hashCode() % tableSize);
}
//"double" the table size and reinsert the values stored in the
//current table. the table size should remain prime
private void resize() {
}
//returns true when e is in the set, otherwise returns false
public boolean find(Integer e) {
return false;
}
//if e is not in the set add e to the set otherwise the set does not change
//if after adding the new element numElements > 2*tableSize then call resize
public void addElement(Integer e) {
}
//returns a string representation for the set
//the string representation of the set is { followed by a comma delimiter list of set
//elements followed by a }. The string for the empty set is {}
//For example, {1,2,3}.
//Note that you SHOULD NOT have any spaces in your String
/*
* Example:
* table[0]: 2 -> 4
* table[1]: 1 -> 3
*
* The string representation of this set is {2,4,1,3}
*/
public String toString() {
}
public class MySetIterator implements Iterator<Integer> {
//implements an iterator for the set
private int currentList;
private Node currentNode;
//helper method that finds the next non empty bucket
private void nextList() {
while (currentList < tableSize && table[currentList] == null) {
currentList++;
}
currentNode = (currentList < tableSize) ? table[currentList] : null;
}
public MySetIterator() {
currentList = 0;
nextList();
}
//returns true if the iteration has more elements.
public boolean hasNext() {
return currentNode != null;
}
//Returns the next element in the iteration.
public Integer next() {
Integer rVal = currentNode.element;
if (currentNode.next != null) {
//what should the currentNode be?
}
else {
//No more elements in the current bucket
//I need to get the next bucket
}
return rVal;
}
}
public Iterator<Integer> iterator() {
//returns an iterator for the set
return new MySetIterator();
}
}
![private void resize()
This method "doubles" the table size and reinserts the values stored in the current table. The table size
should remain prime.
private boolean find (Integer e)
This method returns true if the integer e is in the set and false otherwise.
private void addElement (Integer e)
If e is not in the set, add e to the set, otherwise the set does not change. If after adding the new element
numElements > 2*tableSize then call resize. This helps keep searching, inserting, and deleting into the set
fast.
private String toString()
Returns a string representation for the set. The string representation of the set is { followed by a comma
delimiter list of set elements followed by a }. The string for the empty set is {}. You should use the iterator
you finish creating as described in the code.
public class MySetIterator {...}
Finish implementing the MySetIterator class to create an iterator for your set. This will allow you to iterate
through your set when creating a String representation. See the comments in the code for more
instructions.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F165f5551-99fc-4800-9810-1597ce53807c%2F93beea7d-e790-4808-a0f2-f2d0f81e43cc%2Fmcavdc_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)