Solved

What is the best way to serialize multiple dissimilar objects

Posted on 2011-09-20
6
233 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now