I thought that went without saying and you were wanting to use the font for something else, heh. I want to try it out as my phone's system font, actually...
My guess is that HashSet implements its own serialization logic as an optimisation: since the objects in the set are stored as keys in the map, serializing the values is useless and wasteful.
Thus the map is marked as transient, and HashSet implements the methods writeObject and readObject to provide its own serialization that ignores the values in the map.
Sorry if I don't know what I'm talking about, but wouldn't implementing read/writeObject, be enough to prevent the built in serialization from being used?
I found out about it a while ago, it seems clever for reuse but not so much for efficiency because every stored object comes with an unused null reference and the heap gets polluted by pairs of them (Map.Entry) even since there's no need for actual pairs.
From a theoretical data structure point of view the opposite makes much more sense: defining HashMap in terms of HashSet of pairs but only using the keys of the pairs for the entire internal logic. This is how any kind of map structure is really constructed in theory because it follows an intuitive bottom up construction manner: creating complex structures from simpler ones. This construction also is weird in this manner that it does the opposite, which doesn't make much sense.
Well, from a practical perspective, Rust does the same thing as Java, where HashSet<T> is implemented as HashMap<T, ()>, but since the unit type () is a zero sized type, it takes up no memory, and any code that would handle it is optimized out, leading to an implementation as efficient as a handcrafted one.
Do you know of any languages that implement it that way? Most languages that I can think of off the top of my head consider a hashtable to be a simpler data structure and implement set either as a hashtable or a binary tree. Examples include Java, c++, python, Haskell.
They are special pairs like that because in a map, keys are unique. A set of classical pairs would have unique pairs, which would allow for same key with multiple values.
Yeah. That's not how HashSets work. E.g. if the element (perhaps, a pair, special or not) is not in the set, you can add it and if you do other elements are not affected and so on. Which is probably the reason HashSets are backed by HashMaps, not the other way around.
I have no idea what you're trying to say with that because even HashMap implementation keeps key-element pairs internally, placed into buckets or not by only hashing the key. The pairing is only necessary to hold the extra non-hashed related value, without it is a perfectly fine HashSet.
If you'd try to implement a HashMap as a set (perhaps a HashSet) of pairs, you'd either want operations that are not there or you'd have suboptimal performance. Suppose you add a key-value pair, you'd want to see if the key is in the map. The set doesn't allow you to check that efficiently. You can check if something is contained in a set quickly, but not if something is a member of an element of the set.
In a language like Java, the map entry class can implement hashCode by delegating to the key of the pair, making it exactly as efficient. You don't seem to be familiar with this.
7
u/HaniiPuppy Oct 22 '17
What's that font.
12
u/pi_rocks Oct 22 '17
Does anyone know why map is marked as transient?
22
u/sim642 Oct 22 '17
I found out about it a while ago, it seems clever for reuse but not so much for efficiency because every stored object comes with an unused null reference and the heap gets polluted by pairs of them (
Map.Entry) even since there's no need for actual pairs.From a theoretical data structure point of view the opposite makes much more sense: defining
HashMapin terms ofHashSetof pairs but only using the keys of the pairs for the entire internal logic. This is how any kind of map structure is really constructed in theory because it follows an intuitive bottom up construction manner: creating complex structures from simpler ones. This construction also is weird in this manner that it does the opposite, which doesn't make much sense.1
u/[deleted] Oct 22 '17
I was under the impression that most sets were implemented as maps.
1
u/WonderJ13 Jan 24 '18
TIL what transient means in Java
1
u/Tarrjue Oct 22 '17
Yup. Seems obvious in hindsight doesn't it?