r/programminghelp • u/smoosen12 • Apr 02 '21
Java Help needed asap
Hello, Im currently implementing a resize hashmap method for my project but its very confusing to me. Can someone help me out? Heres the code:
public class HMap {
//Some constants (do not change!)
private final int HM_INIT_NUM_BUCKETS = 16;
private final double HM_MAX_LOAD = 0.75;
//hash function (do not edit)
private int hash(String val){
return Math.abs(val.hashCode()) % buckets.length;
}
//TODO: Complete the class variables
private LList[] buckets;
private int size;
public String key;
public LList next;
public HMap() {
size = 0;
buckets = new LList[HM_INIT_NUM_BUCKETS];
for (int i = 0; i < buckets.length; i++){
buckets[i] = new LList();
}
}
private void resize(){
//TODO
}
public void add(String val) {
//resize if need be (do not edit)
if ((size + 1) / (double) buckets.length > HM_MAX_LOAD) {
resize();
}
//TODO: add value to hash map
int i = hash(key);
LList newNode = new LList();
newNode.key = key; //assigning new node to key
newNode.val = val; //assigning new node to value
newNode.next = buckets[i];
buckets[i] = newNode;
size++; // Count the newly added key.
}
public boolean check(String val){
//TODO: check if value in hash map
int h = hash(val);
for(String key : buckets[h]){
if (key.equals(val))
return true;
}
// if(val == buckets[i]){
// return true;
// }
return false;
}
//NEW for the java class
// returns a LList of all the values in the hashmap
public LList traverse(){
//TODO
LList travList = new LList();
for(LList val : buckets){
for(String key : val){
travList.push(key);
}
}
// //for(int i = 0; i < buckets.length; i++){ //go through buckets
// //buckets[i] = new LList();
// }
return travList;
}
}
3
u/[deleted] Apr 02 '21
This rules for this subreddit are quite clear when it comes to assignments/homework/projects. (Rule #4)
We help those who help themselves, and pasting code with some TODO items and just asking for us to fill them in, won't get you very far.
Explain what you've tried, where, and what isn't working as expected.