Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is the best way to serialize multiple dissimilar objects

Posted on 2011-09-20
6
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

715 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