• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

why does the Serializable HashSet has a transient member map?

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class HashSet<E>
   extends AbstractSet<E>
   implements Set<E>, Cloneable, java.io.Serializable
{
   static final long serialVersionUID = -5024744406713321676L;

   private transient HashMap<E,Object> map;
   ...
}

since map contains the data of the HashSet, now map has been transient,  is it still meaningful for HashSet to be serializable?
 
author
Posts: 23845
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason the underlying map is transient is because the developers chose to serialize the elements of the map with the readObject() and writeObject() methods. Perhaps, it is more efficient to do so, instead of letting the default implementation do it?

Regardless, have you tried it? If so, you will see that it works. The elements do get serialized and deserialized.

Henry
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:The reason the underlying map is transient is because the developers chose to serialize the elements of the map with the readObject() and writeObject() methods. Perhaps, it is more efficient to do so, instead of letting the default implementation do it?

Regardless, have you tried it? If so, you will see that it works. The elements do get serialized and deserialized.

Henry


what if the developers just want to choose to serialize the hashset itself because it's much more convenient? for what reason the api designers do not allow them to do so?
 
Henry Wong
author
Posts: 23845
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

drac yang wrote:
what if the developers just want to choose to serialize the hashset itself because it's much more convenient? for what reason the api designers do not allow them to do so?



I am not sure if I understand your issue here. What does the HashSet API have to do with this? This is an implementation detail. As long as the readObject() and the writeObject() methods are "balanced", the serialization and deserialization should work correctly.

Keep in mind, that "transient" does *not* mean that it will not be serialized. Marking something as transient just means that the default mechanism does not serialize the field. In this case, the map is still serialized, the developer of the HashSet class just chose to serialize it differently.


Again, just test it. You will see that serializing and deserializing a hashset work fine.

Henry
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
Keep in mind, that "transient" does *not* mean that it will not be serialized. Marking something as transient just means that the default mechanism does not serialize the field. In this case, the map is still serialized, the developer of the HashSet class just chose to serialize it differently.

Again, just test it. You will see that serializing and deserializing a hashset work fine.

Henry


thank you very much, i tried, and succeeded in deserializing the HashSet object, serialize process is as follows:



but I'm still wondering why it could be deserialized since the map of the HashSet is transient.
or how can the transient map functions by the transient modifier?
 
Henry Wong
author
Posts: 23845
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

drac yang wrote:I'm still wondering why it could be deserialized since the map of the HashSet is transient.
or how can the transient map functions by the transient modifier?



Well, since you are already looking at the source code, look a bit further down, at the writeObject() and readObject() methods respectively. These are the methods that serializes and deserializes the HashSet.

Notice that these methods call the default methods -- which will serialize/deserialize everything but not the static or transient fields. Then, later in the methods, the map is serialized/deserialized by the methods.

Henry
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Well, since you are already looking at the source code, look a bit further down, at the writeObject() and readObject() methods respectively. These are the methods that serializes and deserializes the HashSet.

Notice that these methods call the default methods -- which will serialize/deserialize everything but not the static or transient fields. Then, later in the methods, the map is serialized/deserialized by the methods.

Henry



after your detailed explaining, i think i got it, thanks a lot.

in this case, i think, if the serialize/deserialize process of the HashSet does not involve the invocation of its own readObject/writeObject, then it just cannot transfer the data of the HashSet.
but in which situation will this happen?

look at this code:

i guess eventually the HashSet property of person object has been "writeObject"ed by using the writeObject method of HashSet, right?


 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!