This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Serializing classes with hashmaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Serializing classes with hashmaps" Watch "Serializing classes with hashmaps" New topic
Author

Serializing classes with hashmaps

S. William Smith
Greenhorn

Joined: Oct 07, 2010
Posts: 4
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.

-Will
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

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
Greenhorn

Joined: Oct 07, 2010
Posts: 4
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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serializing classes with hashmaps
 
Similar Threads
De-Serialization is returning Null values
please explain the output
ObjectOutputStream Make copy of Serialized File
Why are transient static variables allowed?
Store a linked list into a file