It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. It is confusing that all of them implement the hash table. Do they implement the concept of hash table? Java's Set and Map interfaces specify two very different collection types.
A Set is just what it sounds like: a collection of distinct non-equal objects, with no other structure. A Map is, conceptually, also just what it sounds like: a mapping from a set of objects the distinct keys to a collection of objects the values.
Hashtable is a legacy class that almost always should be avoided in favor of HashMap. They do essentially the same thing, except most methods in Hashtable are synchronized, making individual method calls thread-safe.
The problem with Hashtable is that synchronizing each method call which is a not-insignificant operation is usually the wrong thing. Either you don't need synchronization at all or, from the point of the view of the application logic, you need to synchronize over transactions that span multiple method calls.
Since it was impossible to simply remove the method-level synchronization from Hashtable without breaking existing code, the Collections framework authors needed to come up with a new class; hence HashMap. It's also a better name, since it becomes clear that it's a kind of Map. Oh, if you do need method-level synchronization, you still shouldn't use Hashtable.
Instead, you can call Collections. Alternatively, you can use ConcurrentHashMap , which, according to the docs : "obeys the same functional specification as Hashtable " but has better performance and additional functionality such as putIfAbsent. In terms of functionality, HashSet has nothing to do with HashMap. It happens to use a HashMap internally to implement the Set functionality. For some reason, the Collections framework developers thought it would be a good idea to make this internal implementation detail part of the public specification for the class.
This was an error, in my view. Hashtable was an old class that was created before Java had generics. It's only around still for backwards compatibility. In the HashSet, there must be no duplicate elements. Skip to content. Change Language. Related Articles.
Table of Contents. Save Article. Improve Article. Like Article. Output Before adding duplicate values [practice, geeks, contribute] After adding duplicate values [practice, geeks, contribute] After adding null values [null, practice, geeks, contribute].
Both are using hashing technique to store unique keys. JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week. Next Topic Java EnumSet. Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network.
Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering.
0コメント