The difference between HashMap, Hashtable and HashSet in Java
Hashtable class
Hashtable inherits the Map interface and implements a hash table of key-value mapping. Any non-null object can be used as key or value.
To add data, use put(key,value), and to remove data, use get(key). The time cost of these two basic operations is constant.
Hashtable adjusts performance through two parameters: initial capacity and load factor. Usually the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor can save space but the corresponding search time will increase, which will affect operations like get and put.
A simple example of using Hashtable is as follows. Put 1, 2, and 3 into Hashtable, and their keys are "one", "two", and "three" respectively:
Hashtable numbers = new Hashtable( );
numbers.put(“one”, new Integer(1));
numbers.put(“two”, new Integer(2));
numbers.put(“three”, new Integer(3));
To take out a number, such as 2, use the corresponding key:
Integer n = (Integer)numbers.get(“two”);
System.out. println(“two = ” + n);
Since the object as the key will determine the position of the corresponding value by calculating its hash function, any object as the key must implement hashCode and equals method. The hashCode and equals methods inherit from the root class Object. If you use a custom class as a key, be very careful. According to the definition of the hash function, if the two objects are the same, that is, obj1.equals(obj2)=true, then Their hashCode must be the same, but if two objects are different, their hashCode is not necessarily different. If the hashCode of two different objects is the same, this phenomenon is called a conflict. The conflict will cause the time overhead of operating the hash table to increase. Therefore, try to define a well-defined hashCode() method to speed up hash table operations.
If the same object has different hashCode, the operation of the hash table will have unexpected results (the expected get method returns null). To avoid this problem, you only need to remember one thing: copy at the same time equals method and hashCode method, rather than just writing one of them. Hashtable is synchronous.
HashMap Class
HashMap is similar to Hashtable, except that HashMap is asynchronous and allows null, that is, null value and null key. , but when treating HashMap as a Collection (the values() method can return a Collection), the time overhead of its iteration sub-operations is proportional to the capacity of the HashMap. Therefore, if the performance of iterative operations is very important, do not set the initial capacity of HashMap too high or the load factor too low.
WeakHashMap Class
WeakHashMap is an improved HashMap, which implements "weak references" to keys. If a key is no longer referenced externally, the key can be recycled by GC.
HashSet please refer to the description of Set
Set is a Collection that does not contain repeated elements, that is, any two elements e1 and e2 have e1.equals(e2)=false, Set has the most There is a null element.
The constructor of Set has a constraint that the passed-in Collection parameter cannot contain duplicate elements.
Please note: Mutable objects (Mutable Object) must be handled with care. If a mutable element in a Set changes its state causing Object.equals(Object)=true, it will cause some problems.
Two common Set implementations are HashSet and TreeSet. Deciding which one to use is pretty straightforward. HashSet is much faster (constant time vs. log time for most operations), but does not provide ordering guarantees. If you need to use the operations in a SortedSet, or iterating sequentially is important to you, then use a TreeSet. Otherwise, use HashSet. It's a fair gamble for you not to use a HashSet most of the time.
One thing you should keep in mind about HashSet is that iteration is linear in terms of the sum of the number of entries and the capacity. Therefore, if iteration performance is important, an appropriate initial capacity should be chosen carefully. Choosing a capacity that is too large wastes both space and time. The default initial capacity is 101, which is generally more than you need. The initial capacity can be specified using the int constructor. To allocate a HashSet with an initial capacity of 17:
Set s= new HashSet(17);
HashSets also have a "tuning parameter" called the load factor. " . If you are very concerned about the space usage of your HashSet, read the HashSet text for details. Otherwise, just use the default value. If you accept the default load factor, but you do want to specify an initial capacity, pick a number that is approximately twice the capacity you expect your Set will grow to. If your guess is off base, it can grow, or just waste a little space. But there are no big problems. If you know the best value for the correct size, use it; if you don't know, use an old value, or use an even value. It's really not very important. These things only make HashSet slightly better.
TreeSet has no adjustment parameters. In addition to clone, HashSet and TreeSet have only those operations required by their respective interfaces (Set and TreeSet), and no other operations.
For more articles related to the differences between HashMap, Hashtable and HashSet in Java, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

Start Spring using IntelliJIDEAUltimate version...

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...
