如何在hashmap中找到键时的标识:entry.getBname.getTitle()"

时间:2014-09-25 09:24:49

标签: java hashmap key-value put

当我进行hashmap编码时,我碰到了一个凸起以识别密钥,如下所示:

public void addBE(BookEntry entry)
{
library.put(entry.getBname().getTitle(), entry);
}

因此,对于此示例,getBname()getTitle是否为此键?并且条目应该是正确的值?但是条目代表一个类,所以它意味着条目中的一切都是值?

感谢您澄清我的疑问。

仅供参考:BookEntry类:

public class BookEntry{
private BookName bname;
private Writer wname;
private BookID b_id;

public BookEntry(BookName bname, Writer wname, BookID b_id)
{
this.bname = bname;
this.wname = wname;
this.b_id = b_id;
}

public BookName getBname()
{
return bname;
}
public Writer getWname()
{
return wname;
}
public BookID getBookID()
{
return b_id;
}
public String toString()
{
return bname.toString() + " " + wname.toString() + " " + b_id.toString();
}

public static BookEntry enterBE()
{
BookName bname = BookName.enterName();
Writer wname = Writer.enterWriter();
BookID b_id = BookID.enterID();
return new BookEntry(bname, wname, b_id);
}
}

3 个答案:

答案 0 :(得分:2)

键是标题,即getTitle()的输出。它将与:

相同
String titleKey = entry.getBname().getTitle(); // assuming title is a String, but it could be any type
library.put(titleKey, entry);

答案 1 :(得分:0)

是的,您的理解是正确的。整个入门类是一个值。

在下面一行

library.put(entry.getBname().getTitle(), entry);

entry.getBname().getTitle() - >关键

entry --->值。

答案 2 :(得分:0)

Java Map<Key,Value>

现在在这里

library.put(entry.getBname().getTitle(), entry); // library is your Map

entry.getBname().getTitle()返回的值是Keyentry是值。