• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Serialize every new entry in a HashMap

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Any idea how I can serialize every single entry in a HashMap to a single file?
For example, say I when I add a new entry to a HashMap the entry should serialize in a file and not the entire HashMap.


Regards,
Damodar
 
Sheriff
Posts: 22841
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should create a custom Map implementation (extends HashMap if you want) that overrides put and putAll, but also remove, clear, removeAll and retainAll not only of the Map but also its entrySet, keySet and values.
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob, but how serialize will work here?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Rob, but how serialize will work here?



To make your serialize work, you may over-ride readObject() and writeobject() method which should only read and write only updated keys and values from HashMap.
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I write the updated object into an existing serialized file?
Say the .ser file already has 10 HashMap Key value pair now can I add one more into it?
Could you please give an example?

Regards,
Damodar
 
Sheriff
Posts: 28384
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try it?

I don't see why you need an example, you already know how to serialize an object to a file. Don't you?
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but I am afraid, if the system crash and not able to close the Stream? also do I require flash() after avary new entry in the .ser file?
reply
    Bookmark Topic Watch Topic
  • New Topic