I am trying to write a small program that will allow me to add a person's name plus his or her birthday.
I would like the following things to be allowed:
find(name) returns a birthday
add(name, birthday)
and finally a remind(birthday) and this will return a list of people who have this birthday.
I'm also trying to use the Java 1.5 collections library to do this.
So far, I'm thinking of using a HastTable to keep name, birthday pairs. The adding and finding methods are real easy to do using the built in methods for Hasttable. The problem I'm having is the remind method. One way to do it is to just iterate through the whole hasttable and pull out all those who have the same birthdays. Another way would be to keep another Hasttable using the birthdays as keys and an ArrayList with names as the value.
The first way of doing this would make the remind method kind of ugly. But would save the space of not having to replicate another HastTable with the same values. The second way of doing this would make all the methods real easy to implement, but would waste space with another hash table that has the exact same values.
So finally, the question. Is there a collection that will allow me to look up a value given a key and look up a key given a value all in O(log n) time?
thanks!
Disclaimer: This is a school assignment, I could probably whip this up just using arrays and my own implementation of said methods. But we have to use the libraries that Java gives us. I am not asking for code or anything, just a nudge in the right direction in terms of the data structure I should be using.
I would like the following things to be allowed:
find(name) returns a birthday
add(name, birthday)
and finally a remind(birthday) and this will return a list of people who have this birthday.
I'm also trying to use the Java 1.5 collections library to do this.
So far, I'm thinking of using a HastTable to keep name, birthday pairs. The adding and finding methods are real easy to do using the built in methods for Hasttable. The problem I'm having is the remind method. One way to do it is to just iterate through the whole hasttable and pull out all those who have the same birthdays. Another way would be to keep another Hasttable using the birthdays as keys and an ArrayList with names as the value.
The first way of doing this would make the remind method kind of ugly. But would save the space of not having to replicate another HastTable with the same values. The second way of doing this would make all the methods real easy to implement, but would waste space with another hash table that has the exact same values.
So finally, the question. Is there a collection that will allow me to look up a value given a key and look up a key given a value all in O(log n) time?
thanks!
Disclaimer: This is a school assignment, I could probably whip this up just using arrays and my own implementation of said methods. But we have to use the libraries that Java gives us. I am not asking for code or anything, just a nudge in the right direction in terms of the data structure I should be using.