DB4o HashMap toString()

发布于 2024-11-03 00:42:41 字数 2522 浏览 0 评论 0原文

作为 DB4o 和 Java 的学习工具,我已经开始创建电话目录。为此,我创建了一个 TelephoneDirectory 实例,其中包含年份和条目的 HashMap。

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);
  }
}

因此,我使用 addEntry 添加了一些条目。我想做的是在电话簿中搜索特定的姓名。为此,我使用 QueryByExample (QBE),如下所示:

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());
  }
}

我遇到的问题是,如果在哈希图中找到结果,那么我需要打印键/值对。到目前为止的输出是:

尺寸:1 电话目录.TelephoneDirectory@da4b71

这显然是因为没有 toString() 方法。但是我应该在 toString() 方法中放入什么,因为结果中只会出现哈希映射值的子集。

示例

TelephoneDirectory dir = new TelephoneDirectory(2011);
dir.addEntry("12345", "Adam");
dir.addEntry("67890", "Bob");
dir.addEntry("24680", "Carl");

然后我查询:

lookupName("Bob", 2011, db);

预期结果:

2011 - 67890:鲍勃

,我确信我忽略了一些简单的事情。

提前致谢。

编辑:我刚刚意识到我正在使用 ObjectSet 作为 QBE 的结果。它似乎没有让我对这个问题有更多的了解,但也许它提供了不同的实现方法?

更新:基于PeterMmm的努力,我现在将他的回复调整为以下内容:

  @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();
  }

当然,这里的问题是toString方法仍然使用整个实例hashmap,即hashmap.keySet() ; 而不是 ObjectSet 结果,因此,在搜索 “bob” 时,将返回所有结果,即“Adam、Bob 和 Carl”

完整内容答案: 问题现已解决,但仅部分归因于给出的答案,因此我将其标记为最佳答案,但在下面给出完整的详细信息。

我不仅应该包含 toString,还需要操作 ObjectSet,并且因为查询基于 TelephoneDirectory,所以我能够强制转换 ObjectSet 返回到 TelephoneDirectory。通过这样做,我可以使用它自己的实例方法来操作 TelephoneDirectory

非常感谢您付出的时间和精力。

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);
dir.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2024-11-10 00:42:41

实现目录的另一种方法是:

public class TelephoneEntry {
  private int year;
  private String name;
  private String tel;

  ....
}

您通过 hashmap 管理的集合将由 db4o 您管理。

更新经过简短的讨论,您可以覆盖 TelephoneDirectory.toString() :

class TelephoneDirectory {

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Object k : hashmap.keySet()) {
            // details for you 
        }
        return sb.toString();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文