When I was doing hashmap coding, I hit a bump to identify key, an example as follow:
public void addBE(BookEntry entry)
{
library.put(entry.getBname().getTitle(), entry);
}
So for this example, is getBname()
or getTitle
the key for this?, and entry should be the value right? But entry represents a class, so it means everything in entry is the value?
Thanks for clarifing my doubt.
FYI: BookEntry class:
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);
}
}
The key is the title, i.e. the output of getTitle(). It would be the same as:
String titleKey = entry.getBname().getTitle(); // assuming title is a String, but it could be any type
library.put(titleKey, entry);