The difference between HashMap and Hashtable and HashSet in Java _java

Source: Internet
Author: User
Tags constant

Hashtable class
Hashtable inherits the map interface and implements a hash table of key-value mappings. Any object that is Non-null (non-null) can be a key or value.

Adding data using put (Key,value), fetching data using get (key), the time cost of these two basic operations is constant.

Hashtable adjusts performance by initial capacity and load factor two parameters. Typically, the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor saves space but the corresponding lookup time increases, which can affect operations like get and put.

Using Hashtable for example, put 1,2,3 into Hashtable, their key is "one", "two", "three":
Hashtable numbers = new Hashtable ();
Numbers.put ("One", New Integer (1));
Numbers.put ("Two", New Integer (2));
Numbers.put ("Three", New Integer (3));

To remove a number, such as 2, use the corresponding key:
Integer n = (integer) numbers.get ("two");
System.out.println ("two =" + N);

Because an object that is a key will determine the location of its corresponding value by calculating its hash function, any object that is a key must implement the Hashcode and Equals methods. The hashcode and Equals methods inherit from the root class object, and if you use a custom class as a key, be very careful, as defined by the hash function, if two objects are the same, that is, Obj1.equals (OBJ2) =true, Their hashcode must be the same, but if two objects are different, their hashcode is not necessarily different, and if the hashcode of two different objects is the same, this phenomenon is called conflict, and the conflict causes the time overhead of manipulating the hash table, so as to define the hashcode () method to speed up the operation of the hash table.

If the same object has a different hashcode, the operation of the hash table will have unexpected results (the expected GET method returns null), to avoid this problem, you need to keep in mind one: you have to copy both the Equals method and the Hashcode method, not just one. The Hashtable is synchronized.

HashMap class
HashMap is similar to Hashtable, except that HashMap is unsynchronized and allows NULL, that is, null value and null key. , but when HashMap is treated as collection (the values () method returns collection), its iterative child operation time cost is proportional to the capacity of the HashMap. Therefore, if the performance of an iterative operation is significant, do not set the HASHMAP initialization capacity too high, or the load factor too low.

Weakhashmap class
Weakhashmap is an improved hashmap, which implements a "weak reference" to key, and if a key is no longer referenced externally, the key can be reclaimed by GC.

HashSet please refer to the description of the set
A set is a collection that contains no duplicate elements, that is, any two elements E1 and E2 have E1.equals (E2) =false,set have a maximum of one null element.

The constructor of the set has a constraint, and the Passed-in collection parameter cannot contain duplicate elements.
Note: You must be careful to manipulate variable objects (mutable object). If a variable element in a set changes its state, causing the Object.Equals (Object) =true to cause some problems.

Two common set implementations are HashSet and TreeSet. It is very straightforward to decide which one to use.   The HashSet is much faster (the constant time for most operations is the logarithmic time (constant vs.). Log time), but does not provide a sort guarantee.   If you need to use an action in SortedSet, or an iterative iteration is important to you, use TreeSet.   Otherwise, use HashSet. It's a fair bet for you to not use HashSet most of the time.

One thing to remember about HashSet is that the iteration is linear in terms of the sum of the entries and the volume. Therefore, if iterative performance is important, it should be prudent to choose an appropriate initial capacity.   Capacity is too large to waste space and time. The default initial capacity is 101, generally speaking, it is more than you need. You can use the Int constructor to specify the initial capacity. The initial capacity to allocate HashSet is 17:

Set s= New HashSet (17);

Hashsets also has an "adjustment parameter (tuning parameter)" called the load factor (load factor). If you are very concerned about the use of your hashset space, please read the hashset text for more information. Otherwise, use the default value. If you accept the default load factor, but you do want to specify the initial capacity, choose a number that is about twice times the size you expect your set to grow. If you don't have a guess, it can grow, or just waste a little space. But there's no big problem. If you know an optimal value for the correct size, use it, or, if you don't know, an old value, or use an even number. It's really not very important. These things only make hashset a little bit better.

TreeSet No adjustment parameters. In addition to clone, HashSet and TreeSet have only those operations (set and TreeSet) that are required by their respective interfaces, without any other action.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.