DB4o HashMap toString()
As a learning tool for DB4o and Java I have started to create a Telephone Directory. To do this I create an instance of a TelephoneDirectory which contains a year and a HashMap of entries.
public class TelephoneDirectory {
private int year;
private HashMap<String, String> hashmap;
public TelephoneDirectory(int year) {
this.year = year;
this.hashmap = new HashMap<String, String>();
}
public int getYear() {
return year;
}
public HashMap getHashmap() {
return hashmap;
}
public void addEntry(String name, String number) {
hashmap.put(number, name);
}
}
So I add a few entries with addEntry
. What I would like to do is search through the telephone directory for a specific name. For this I use QueryByExample (QBE), like so:
public static void lookupName(String name, int year, ObjectContainer db) {
TelephoneDirectory proto = new TelephoneDirectory(year);
proto.addEntry(name, null);
ObjectSet result=db.queryByExample(proto);
System.out.println("Size:" + result.size());
while(result.hasNext()) {
System.out.println(result.next());
}
}
The issue that I am having with this is that if a result is found in the hashmap, then I need the key/value pair to be printed. So far the output is:
Size:1 telephonedirectory.TelephoneDirectory@da4b71
This is obviously because there is no toString()
method. But what do I put in the toString() method as only a subset of hashmap values will be present in the result.
Example
TelephoneDirectory dir = new TelephoneDirectory(2011);
d开发者_如何学编程ir.addEntry("12345", "Adam");
dir.addEntry("67890", "Bob");
dir.addEntry("24680", "Carl");
And I then query:
lookupName("Bob", 2011, db);
Expected Result:
2011 - 67890: Bob
I am sure that it is something simple that I am overlooking.
Thanks in advance.
EDIT: I just realized that I am using an ObjectSet as the result of QBE. It doesn't seem to give me any more light on the problem, but maybe it provides a different implementation method?
UPDATE: Based on the efforts of PeterMmm I have now adjusted his reply to the following:
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(year + "\n");
for (Object k : hashmap.keySet()) {
sb.append(k + ": " + this.lookupName((String)k) + "\n");
}
return sb.toString();
}
The problem here of course is that the toString method still uses the whole instance hashmap i.e. hashmap.keySet();
instead of ObjectSet result
and so, on a search of "bob"
all results are returned i.e. "Adam, Bob and Carl"
The FULL answer: The problem has now been resolved but only partly due to the answer given so I will mark it as the best answer but give full details below.
Not only should I include the toString I also needed to manipulate the ObjectSet
, and because the query was based over a TelephoneDirectory
I was able to cast the ObjectSet
back to a TelephoneDirectory
. In doing this I was then able to manipulate the TelephoneDirectory
using it's own instance methods.
Thank you very much for your time and effort.
Anothe way to implement the directory would be:
public class TelephoneEntry {
private int year;
private String name;
private String tel;
....
}
The collection that you manage thru hashmap will be managed by db4o for you.
Update After short discussion, you may override TelephoneDirectory.toString() :
class TelephoneDirectory {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Object k : hashmap.keySet()) {
// details for you
}
return sb.toString();
}
}
精彩评论