Results 1 to 5 of 5
  1. #1
    adamholmes is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Question 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.
    Java Code:
    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:

    Java Code:
    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

    Java Code:
    TelephoneDirectory dir = new TelephoneDirectory(2011);
    dir.addEntry("12345", "Adam");
    dir.addEntry("67890", "Bob");
    dir.addEntry("24680", "Carl");
    And I then query:
    Java Code:
    lookupName("Bob", 2011, db);
    Expected Result:

    2011 - 67890: Bob
    I am sure that it is something simple that I am overlooking.

    Thanks in advance.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    An person class would be very helpful, however; what happens if you get the key I'm the print line?

  3. #3
    adamholmes is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    An person class would be very helpful, however; what happens if you get the key I'm the print line?
    I'm not quite sure I understand what you mean with the last bit?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not really familiar with databases, however; it seems that you are printing a telephone directory, if you can get access to the map you can find the name of the person in the map, correct?


    I apologize if I am being a bit unclear. You gave yourself the ability to access the year and map in the directory, your result.next is returning a telephone directory so you can access the information.
    Last edited by sunde887; 04-26-2011 at 01:44 PM.

  5. #5
    adamholmes is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Well the QBE returns an ObjectSet which contains the results and hence I shouldnt need to find they key/value pairs. They should already be there, its just a case of displaying them...

Similar Threads

  1. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  2. Transparent pesistence on Eclipse with db4o
    By german in forum Java Software
    Replies: 2
    Last Post: 05-26-2009, 04:53 AM
  3. Persisting POJOs on CDC with db4o
    By german in forum CDC and Personal Profile
    Replies: 0
    Last Post: 05-12-2009, 08:18 PM
  4. Problem with Eclipse and Db4o
    By Paro in forum Eclipse
    Replies: 0
    Last Post: 06-27-2008, 12:36 PM
  5. db4o 6.4
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-03-2007, 06:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •