Hi, I'll be up front and say I'm a CS student who is trying to complete a homework assignment.
I have been tasked to serialize an object (named Library) and saving to a file on disk.
Library contains only one object- HashMap<Double, Book> where Book is another class in the project, and the Double value consists of the book's ISBN number
On that note, the Book object consists of two primitive int values, an object of type "Title", and an ArrayList<Borrower> where Borrower is another class in the project.
Title consists of two String objects and a double value
Borrower consists of one String object and two Calendar objects
Library is a singleton class
I have added the import statement and "implements Serializable" in all of these classes, but nothing else.
I plan to try make the serialization safer after I successfully serialize the Library class using the default method.
Here's the issue: I'm having a problem serializing the Library class. When I serialize this class, the file created only contains one short line which basically identifies the Library object as...who knows (I think- it's hard to read serialized data in NotePad). When I serialize only the HashMap object, however, I am able to almost perfectly duplicate the Library to disk. I thought all was well until I realized the information in the Book class' ArrayList<Borrower> object was not persisting.
So there's something else I need to learn still! I have considered that the problem I'm having is that the generic or default serialized form of the Library and Book classes do not properly package the object, but I don't know how to build a custom serialization object. I have the text "Effective Java", J. Bloch, but it isn't very much help on this subject. Bloch seems to assume that the reader will know how to do this already. Other readers might...I don't!
Here's my methods so far:
Is there something simple concerning HashMaps and ArrayLists that I'm missing here?
Thanks for any thoughts / direction offered on this.
You didn't post the code for the Library class, but I would hazard a guess that a Library object doesn't contain the HashMap you think it does.
S. William Smith
Joined: Oct 07, 2010
Well, most of the Library class is just private methods but here's the meat:
Also, the previous serialization code is within the Library class. "books" is the name of the HashMap<> object.
I just figured out what is wrong. The HashMap is a static object and static objects can't be serialized. Well, that's an easy fix in this class because I created the Library as a singleton...so the static HashMap was redundant, anyway. Thanks, Paul- sometimes I need to bounce thoughts off of someone!
Now, although its not a project requirement, I hope someone can help me implement the "Serialization Proxy" recommended by Bloch in Item 78 of "Effective Java".