如何实现迭代器< e>哈希集类别中的迭代方法?
我需要创建此标签集类,除了实现迭代方法外,我已经完成了所有工作如何做的损失。我对迭代器并不完全熟悉迭代器,特别是对于标签/哈希图,因此任何建议都会有所帮助,我认为它非常简单。
public class HashSet<E> implements Set<E> {
private final int[] primes = {100663319, 201326611, 402653189,805306457, 1610612741};
private static final double LOAD_THRESHOLD = 0.75;
LinkedList<E>[] buckets;
int numEntries;
//Parameters for Multiply-Add-Divide compression
//hash function = ((hashCode() * a + b) % p) % buckets.length
int a;
int b;
int p;
public HashSet() {
initSet(16);
}
private void initSet(int bc) {
//Init the array
this.buckets = new LinkedList[bc];
this.numEntries = 0;
//Init the linked lists (buckets)
for(int i=0; i < buckets.length; i++)
{
buckets[i] = new LinkedList<>();
}
//Init MAD param
this.p = primes[(int) (Math.random() * primes.length)];
this.a = (int)(Math.random() * (p-1) + 1);
this.b = (int)(Math.random() * p);
}
private int hashFunction(E e)
{
// implements the MAD method of compression
return (Math.abs(e.hashCode() * a + b) % p) % this.buckets.length;
}
private int getIndex(E e) {
return hashFunction(e);
}
@Override
public Iterator<E> iterator() {
}
private void expandIfNeeded() {
if ((double) numEntries/buckets.length > LOAD_THRESHOLD) {
// save a reference to old buckets
LinkedList<E>[] oldBuckets = buckets;
// set up a new array of buckets (twice as large), reinitialize
initSet(buckets.length*2);
// iterate through old buckets, adding each entry to new buckets
for (LinkedList<E> bucket: oldBuckets) {
for(E e : bucket)
{
buckets[hashFunction(e)].add(e);
numEntries++;
}
}
}
}
@Override
public boolean add(E e) {
expandIfNeeded();
int index = hashFunction(e);
LinkedList<E> bucket = buckets[index];
for(E elem : bucket)
{
if(elem.equals(e))
{
return false;
}
}
// If the key doesn't already exist in the set,
// put the new entry into its bucket.
bucket.addFirst(e);
numEntries++;
return true;
}
@Override
public boolean remove(E e) {
int index = hashFunction(e);
LinkedList<E> bucket = buckets[index];
for(E elem : bucket)
{
if(elem.equals(e))
{
bucket.remove(e);
numEntries--;
return true;
}
}
return false;
}
@Override
public boolean contains(E e) {
int index = hashFunction(e);
LinkedList<E> bucket = buckets[index];
for(E elem : bucket)
{
if(elem.equals(e))
{
return true;
}
}
return false;
}
@Override
public void addAll(Set<E> T) {
Iterator<E> itr = T.iterator();
while(itr.hasNext()) {
this.add(itr.next());
numEntries++;
}
}
@Override
public void retainAll(Set<E> T) {
Iterator<E> itr = this.iterator();
E currentVal = null;
while(itr.hasNext()) {
currentVal = itr.next();
if (!T.contains(currentVal)) {
this.remove(currentVal);
numEntries--;
}
}
}
@Override
public void removeAll(Set<E> T) {
Iterator<E> itr = T.iterator();
E currentVal = null;
while(itr.hasNext()) {
currentVal = itr.next();
if (this.contains(currentVal)) {
this.remove(currentVal);
numEntries--;
}
}
}
public String toString()
{
String r = "";
int largestBucket = 0;
for(int i=0; i < buckets.length; i++)
{
if(this.buckets[i].size() > largestBucket)
{
largestBucket = this.buckets[i].size();
}
r += "Bucket " + i + "( " + this.buckets[i].size() + " ) - ";
for(E e : this.buckets[i])
{
r += e + " ";
}
r += "\n";
}
r += "\nNumber of Entries: " + this.numEntries + "\nLargest Bucket: " + largestBucket + "\nLambda = " + (double)this.numEntries/buckets.length;
return r;
}
}
I need to create this HashSet class and I've done everything except for implement the iterate method, I've tried looking up source code for the built in HashSet but it doesn't function the same way as mine so I'm currently at a loss for how to do it. I'm not exactly familiar with iterators especially for HashSet/HashMap so any suggestions would be helpful, I'm assuming its quite simple.
public class HashSet<E> implements Set<E> {
private final int[] primes = {100663319, 201326611, 402653189,805306457, 1610612741};
private static final double LOAD_THRESHOLD = 0.75;
LinkedList<E>[] buckets;
int numEntries;
//Parameters for Multiply-Add-Divide compression
//hash function = ((hashCode() * a + b) % p) % buckets.length
int a;
int b;
int p;
public HashSet() {
initSet(16);
}
private void initSet(int bc) {
//Init the array
this.buckets = new LinkedList[bc];
this.numEntries = 0;
//Init the linked lists (buckets)
for(int i=0; i < buckets.length; i++)
{
buckets[i] = new LinkedList<>();
}
//Init MAD param
this.p = primes[(int) (Math.random() * primes.length)];
this.a = (int)(Math.random() * (p-1) + 1);
this.b = (int)(Math.random() * p);
}
private int hashFunction(E e)
{
// implements the MAD method of compression
return (Math.abs(e.hashCode() * a + b) % p) % this.buckets.length;
}
private int getIndex(E e) {
return hashFunction(e);
}
@Override
public Iterator<E> iterator() {
}
private void expandIfNeeded() {
if ((double) numEntries/buckets.length > LOAD_THRESHOLD) {
// save a reference to old buckets
LinkedList<E>[] oldBuckets = buckets;
// set up a new array of buckets (twice as large), reinitialize
initSet(buckets.length*2);
// iterate through old buckets, adding each entry to new buckets
for (LinkedList<E> bucket: oldBuckets) {
for(E e : bucket)
{
buckets[hashFunction(e)].add(e);
numEntries++;
}
}
}
}
@Override
public boolean add(E e) {
expandIfNeeded();
int index = hashFunction(e);
LinkedList<E> bucket = buckets[index];
for(E elem : bucket)
{
if(elem.equals(e))
{
return false;
}
}
// If the key doesn't already exist in the set,
// put the new entry into its bucket.
bucket.addFirst(e);
numEntries++;
return true;
}
@Override
public boolean remove(E e) {
int index = hashFunction(e);
LinkedList<E> bucket = buckets[index];
for(E elem : bucket)
{
if(elem.equals(e))
{
bucket.remove(e);
numEntries--;
return true;
}
}
return false;
}
@Override
public boolean contains(E e) {
int index = hashFunction(e);
LinkedList<E> bucket = buckets[index];
for(E elem : bucket)
{
if(elem.equals(e))
{
return true;
}
}
return false;
}
@Override
public void addAll(Set<E> T) {
Iterator<E> itr = T.iterator();
while(itr.hasNext()) {
this.add(itr.next());
numEntries++;
}
}
@Override
public void retainAll(Set<E> T) {
Iterator<E> itr = this.iterator();
E currentVal = null;
while(itr.hasNext()) {
currentVal = itr.next();
if (!T.contains(currentVal)) {
this.remove(currentVal);
numEntries--;
}
}
}
@Override
public void removeAll(Set<E> T) {
Iterator<E> itr = T.iterator();
E currentVal = null;
while(itr.hasNext()) {
currentVal = itr.next();
if (this.contains(currentVal)) {
this.remove(currentVal);
numEntries--;
}
}
}
public String toString()
{
String r = "";
int largestBucket = 0;
for(int i=0; i < buckets.length; i++)
{
if(this.buckets[i].size() > largestBucket)
{
largestBucket = this.buckets[i].size();
}
r += "Bucket " + i + "( " + this.buckets[i].size() + " ) - ";
for(E e : this.buckets[i])
{
r += e + " ";
}
r += "\n";
}
r += "\nNumber of Entries: " + this.numEntries + "\nLargest Bucket: " + largestBucket + "\nLambda = " + (double)this.numEntries/buckets.length;
return r;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论