I have a program where a user can create a task and set it as a favorite or not. Once that has finished, the user should be able to find it again when entering the name. The problem is, my get method is returning null when used.
I have tried storing it first within a MapEntry, element, but it only leads to a NullPointerException. I have also tried just displaying the hashmap getting the key itself but it only leads to the result being null. My MapEntry class consists of private variables to get the key and value of my Favorites class:
Favorites key;
boolean value;
MapEntries next;
with its constructor being:
public Favorites(String task, boolean bookmark) {
this.task = task;
this.bookmark = bookmark;
}
The Favorites class also has the methods:
@Override
public int hashCode(){
return (bookmark ? 0 :1);
}
@Override
public boolean equals(Object object){
Favorites temp = (Favorites) object;
return this.task.equals(temp);
}
alongside its setters and getters.
and here is the MapEntries class:
public class MapEntries{
Favorites key;
boolean value;
MapEntries entry;
public MapEntries(Favorites key, boolean value) {
this.key = key;
this.value = value;
}
public Favorites getKey() {
return key;
}
public void setKey(Favorites key) {
this.key = key;
}
public boolean isValue() {
return value;
}
public void setValue(boolean value) {
this.value = value;
}
public MapEntries getEntry() {
return entry;
}
public void setEntry(MapEntries entry) {
this.entry = entry;
}
}
Within my custom HashMaps class I have my get method to call in my main program:
First creating the variables at the start of the class:
public class HashMaps{
private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; //16
private MapEntries[] holder = new MapEntries[DEFAULT_INITIAL_CAPACITY];
public MapEntries get(Favorites key) {
if (key == null){
System.out.println("No results...");
return null;
}
int value = key.hashCode();
int holderNumber = getSupplementalHash(value);
if (holder[holderNumber] == null){
System.out.println("No results...");
return null;
}
else{
MapEntries existingElement = holder[holderNumber];
while (existingElement != null) {
System.out.println("Traversing maps for key " + existingElement.getKey());
if (existingElement.key.equals(key)) {
return existingElement;
}
existingElement = existingElement.entry;
}
}
return null;
}
//the get Supplemental Hash method:
private int getSupplementalHash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
}
my method to create a task:
first defining a static hashmap outside of my public static void main method:
static HashMaps hashMaps = new HashMaps();
public static void forFavorites(){
int numberOfTasks = 0;
System.out.println("Enter the number of tasks you want to add: ");
numberOfTasks = Integer.parseInt(key.nextLine());
for (int taskNumber = 1; taskNumber<=numberOfTasks; taskNumber++) {
System.out.println("Task no. " + taskNumber);
System.out.println("\t Enter the new task's title: ");
String newTask = key.nextLine();
String trimSpaces = newTask.trim();
if (trimSpaces.isEmpty() || newTask.length() == 0){
System.out.println("\n Invalid. Title cannot be blank");
}
else {
Favorites toFavorite = new Favorites(trimSpaces, false);
System.out.println("Adding " + toFavorite.getTask());
hashMaps.put(toFavorite, false);
System.out.println("Do you want to favorite this task? Y/N");
String choice = key.nextLine();
if (choice.equalsIgnoreCase("Y")){
System.out.println();
System.out.println();
System.out.println("User chose to add "+
toFavorite.getTask() + " as a favorite");
Favorites favorited = new Favorites(trimSpaces, true);
hashMaps.put(favorited,true);
System.out.println("Press enter to continue..");
key.nextLine();
} else {
System.out.println("Press enter to continue..");
key.nextLine();
}
}
}
System.out.println(hashMaps.toString() + " ");
homeMenu();
}
and finally my search method
public static void search(){
System.out.println("Enter the task you want to find: ");
String lookFor = key.nextLine();
Favorites search = new Favorites(lookFor, false);
System.out.println("\nNumber of tasks found: "+ hashMaps.getSize());
System.out.println(hashMaps.get(search));
System.out.println();
homeMenu();
}
As of now the result is:
Enter the task you want to find:
Task
Number of tasks found: 1
Traversing maps for key Task: false
null