0

I want to make a simple note reminder app that uses hashMap when the date is the key and the value is the text. I have a Panel class(GUI), the hash-table class (Reminder.java), and a "MyDateClass.java" that represent a date for my purposes.

My gui is made of 3 JComboBox (day,month,year), One text area and 2 buttons - "Save", "Load".

The 2 buttons in the GUI Panel:

 butSave.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { MyDateClass chosenDate = new MyDateClass(cbYear.getSelectedIndex()+2013,cbMonth.getSelectedIndex()+1, cb.getSelectedIndex()+1); if(!remind.isReminderExists(chosenDate)){ remind.save(chosenDate, tfReminder.getText()); System.out.println("reminder doesnt exists"); }} }); butLoad.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { System.out.println("tryin to load"); MyDateClass chosenDate = new MyDateClass(cbYear.getSelectedIndex()+2013,cbMonth.getSelectedIndex()+1, cb.getSelectedIndex()+1); if(remind.isReminderExists(chosenDate)){ remind.Load(chosenDate); System.out.println("reminder exists"); }} }); 

Reminder class:

public class Reminder { Map<MyDateClass,String> reminderMap; public Reminder(){ reminderMap = new HashMap<MyDateClass,String>(); } public boolean isReminderExists(MyDateClass date){ return reminderMap.containsKey(date); } public void save(MyDateClass date, String Input){ System.out.println("Trying to save"); reminderMap.put(date, Input); } public void Load(MyDateClass date){ System.out.println("Trying to load"); String output; output = reminderMap.get(date); System.out.println(output); } 

So after i push the save button i get from the console:

Trying to save reminder doesnt exists 

But then i push the Load button for the same date and

if(remind.isReminderExists(chosenDate)) 

Isnt triggerd.

What might be the problem?

Do i need to override hashCode() and equals() ? I genereted them but i dont if and how to change the equals() (do i need to manipulate it to compare both dates? How do i do that if "this" refers to the reminder Object)

3
  • 1
    You need to override hashCode and equals. equals needs to return true if your objects are conceptually equal, and hashCode needs to return a consistent value for a given set your object contents, such that all equals objects will have the same hashCode. Commented Dec 29, 2013 at 20:48
  • You should override hasCode and equals in your MyDateClass code. Commented Dec 29, 2013 at 20:49
  • I override it in the MyDateClass class ? any hint on how to override them? Commented Dec 29, 2013 at 21:23

2 Answers 2

1

Try something like this.

@Override public boolean equals(Object o) { if(o == this) return true; if(!(o instanceof MyDateClass)) return false; MyDateClass that = (MyDateClass) o; // use == for primitives // use .compare for primitive wrappers where available // use .equals for objects return this.ivar1 == that.ivar2 && this.ivar2 == that.ivar2; //etc... } // equal objects must have equal hash codes @Override public int hashCode() { int result = 17; result = 31 * result + ivar1; result = 31 * result + ivar2; return result; } 

Write JUnits to test for reflexive, symmetric, transitive, and consistent results.

Sign up to request clarification or add additional context in comments.

Comments

1

As I know your problem clearly, I think that this function always return false. Because date(parameter) and the object in reminderMap have difference reference. So they can not be equal.

public boolean isReminderExists(MyDateClass date){ return reminderMap.containsKey(date); } 

If you want to use containsKey function of HashMap, maybe you should use String or Number instead of MyDateClass. I mean you should convert object of MyDateClass to String value before inserting it into reminderMap.

public boolean isReminderExists(String date){ return reminderMap.containsKey(date); } 

Otherwise, you can implement your code as Hot Licks mention.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.