views:

41

answers:

4

During serializing objects, can we assign name to different objects? So, that on the time of reading objects, i can call any object by its name and later on can access its members.

I can do it by assigning a unique field to each object and later compare it against that field but that will cost - O(n).

Is there any other way to fast access any particular object, serialized in a file of suppose 100 objects.

Thanks you

+1  A: 

Put them in a map and serialise the map instead?

Noel M
still accessing object cost would be - O(n)
Kuri
If it's a `HashMap` it's `O(1)`
Noel M
If you count the time to deserialize, it's O(n). I think he's looking for a way to jump into the file and pull out just one of the objects, like a database index.
erickson
@erickson, you hit the bullseye.
Kuri
@erickson, do you have any idea about that? Please share it with me
Kuri
A: 

Why do all of the objects have to be in one stream? What if you just save each object in its own file and access the object by file name? If you really need a single file, you could archive them all in a ZIP file, and let it handle the indexing function for you.

erickson
The idea of mapping an arbitrary "name" to a ZIP file entry name makes me uneasy.
Tom Hawtin - tackline
no of objects lies in thousands, that would makes the thing little complex.
Kuri
@Kuri - Your problem says 100 objects, but in any case, you'd use the same code for 2 objects or 10000, so I'm not sure what you mean by making the thing complex.
erickson
@Tom Hawtin - good point. Some name validation or name mangling could be necessary.
erickson
A: 

Maybe it's overkill, but for that use case i would use a database. Something like Berkeley DB sounds appropiate:

http://download.oracle.com/docs/cd/E17277_01/html/GettingStartedGuide/index.html

gpeche
A: 

I think javax.naming package works for it

Thanks to all.

Kuri