Solved

What is the best way to serialize multiple dissimilar objects

Posted on 2011-09-20
6
234 Views
Last Modified: 2012-05-12
I have figured out how to serialize and deserialize a single object including a collection of similar objects.  However, I'm unable to serialize/deserialize multiple dissimilar objects.  I thought about adding all the objects to a generic collection but that failed with compiler warnings and stack traces.

For starters, what is the best way for someone learning about making Java objects persistent, to make dissimilar objects persistent?  (Looking for an explanation of theory in English).

Second, can someone direct me to a code example dissimilar objects are persistent?  All I've been able to find are examples of saving a single object which included a collection of similar objects.  As I said, I don't need help with that.  I can do that.

0
Comment
Question by:Hugh McCurdy
  • 3
  • 3
6 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36569424

You can add them to raw collection
Just declare

ArrayList al = new ArrayList()

You'll see compiler warnings - but that is OK

Just make sure that all your objects are serializable
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569438
ArrayList does not need to have the same objects - and that is  the situation why
they allow to do it.

So don't waorry about these "unchecked" warnings - it will work just as well - as long as you know which of th elements id
of which type when you deserialize them
0
 
LVL 13

Author Comment

by:Hugh McCurdy
ID: 36569453
Hi for_yan, I thought you'd be the first one to post.  (Remember I called you a fast typer?)  I can't run a test now but I plan to tonight (when I'm home).  Thanks.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 47

Expert Comment

by:for_yan
ID: 36569494
Well, I just all the time mutilate English language...
I hope it will forgive me :)
After all over the history some people abused it even worse.

And don't worry - without generics all these collections work just fine.
It was an act of paternalism to create generics - they didn't believe people could be sufficiently organized to keep track of their types on their own.
0
 
LVL 13

Author Comment

by:Hugh McCurdy
ID: 36583645
This appears to work.  The only problem I have now is that I have an object (player) that has another object (room).  (The player is in a whatever room.)  After deserialization, the address of the room isn't the same as it was before serialization.  

Regardless, you have given me a useful answer.  I have an idea on how to solve the new problem.  If that doesn't work, I'll create a new question.  Thanks.
0
 
LVL 13

Author Closing Comment

by:Hugh McCurdy
ID: 36583672
The solution I used is slightly different but you provided valuable assistance.  Primarily, that it is normal to make a collection of serializable objects and cause them to be persistent.

I'm using this project as a self learning project to learn concepts not covered in the Java class I took.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question