HashMap & lt; K, V & gt; generic class

Source: Internet
Author: User

HashMap <K, V> is also a very practical class. The HashMap <K, V> object uses the data structure of the hash list to store data. It is often called HashMap <K, v> the object is a hash ing object. Hash ing is used to store key-value data pairs. It allows you to store any number of key-value data pairs together. Key cannot have logical conflicts. Do not use the same key for two data items. If two data items correspond to the same key, the key-value pairs in the previous hash ing will be replaced. Hash ing automatically increases the capacity when it requires more storage space. For example, if the load factor of hash ing is 0.75, when the capacity of hash ing is 75%, it will increase the capacity to twice the original capacity. For arrays and linked lists, if you want to find a specific element they store but do not know its location, you need to access the element from the beginning until matching is found; if the data structure contains many elements, it will waste time. In this case, it is best to use Hash ing to store the data to be searched. Using hash ing can reduce the retrieval overhead.

1. HashMap <K, V> Object

The objects created by the HashMap <K, V> generic class are called hash ing. For example:

HashMap <String, Student> hashtable = HashMap <String, Student> ();

Hashtable can store key-value pairs. The key must be a String object, and the value corresponding to the key must be a Student object. Hashtable can call the public V put (K key, V value) method to store key-value pairs in hash ing, and return the values corresponding to the key.

2. Common Methods

The common methods for HashMap <K, V> generic classes are as follows.

① Public void clear () -- clear hash ing

② Public Object clone () -- returns a clone of the current hash ing

③ Public boolean containsKey (Object key) -- returns true if hash ing uses the key specified by the parameter. Otherwise, false is returned.

④ Public boolean containsValue (Object value) -- if the hash ing has a key-value Pair value that is specified by the parameter, true is returned; otherwise, false is returned.

⑤ Public V get (Object key) -- return the value of the key-value pair that uses the key as the key in the hash ing.

⑥ Public boolean isEmpty () -- if the hash ing does not contain any key-value pairs, the method returns true; otherwise, false.

7. public V remove (Object key) -- delete the key-Value Pair specified for the parameter in the hash ing and return the value corresponding to the key.

⑧ Public int size () -- returns the size of the hash ing, that is, the number of key-value pairs in the hash ing.

3. Traverse hash ing

To obtain the values of all key-value pairs in the hash ing, use

Public Collection <V> values ()

Method returns a reference to an object created using the <V> Collection interface class, and requires that the object be referenced in the Collection <V> interface variable. The value of all key-value pairs in the hash ing is stored in the objects returned by the values () method, so that the interface variables can call the methods implemented by the class, such as obtaining the Iterator object, then output all values.

 

For example, use the common method of hash ing and traverse the hash ing.

[Java]
Package com. chindroid. date;
 
Import java. util. Collection;
Import java. util. HashMap;
Import java. util. Iterator;
 
Public class TestHashMap {
 
Public static void main (String [] args ){
Book book1 = new Book ("7302033218", "C ++ basics ");
Book book2 = new Book ("7808315162", "Java programming language ");
Book book3 = new Book ("7302054991", "j2's wireless device programming ");

HashMap <String, Book> table = new HashMap <String, Book> ();
Table. put (book1.ISBN, book1 );
Table. put (book2.ISBN, book2 );
Table. put (book3.ISBN, book3 );

String key = "7808315162 ";
If (table. containsKey (key )){
System. out. println (table. get (key). name + "");
}

Book B = table. get ("7302054991 ");
System. out. println ("name:" + B. name + ", ISBN:" + B. ISBN );

Int number = table. size ();
System. out. println ("" + number + "elements in hash ing :");

Collection <Book> collection = table. values ();
Iterator <Book> iter = collection. iterator ();
While (iter. hasNext ()){
Book te = iter. next ();
System. out. printf ("name: % s, ISBN: % s \ n", te. name, te. ISBN );
}
}
}

The program running result is as follows:

Java programming language is available
Title: j2-based wireless device programming, ISBN: 7302054991
Hash ing has three elements:
Title: C ++ basic tutorial, ISBN: 7302033218
Title: j2-based wireless device programming, ISBN: 7302054991
Title: Java programming language, ISBN: 7808315162
 

4. HashMap <E> generic class implementation Interface

The HashMap <E> generic class implements the generic interface Map <E>. Most of the methods are the implementation of the Map <E> interface method. During programming, you can use the interface callback technology to assign a reference to the HashMap <E> object to the Map <E> interface variable. Then, the interface can call the interface method implemented by the class.

Related Article

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.