Hashmap<k, v> generic class

Source: Internet
Author: User

Hashmap<k, V> is also a very useful class, hashmap<k, v> objects using a hash table of this data structure to store information, it is customary to say hashmap<k, v> object is a hash map object. Hash maps are used to store key-value data pairs, allowing any number of key-value data pairs to be stored together. The key can not be a logical conflict, two data items do not use the same key, if there are two data items corresponding to the same key, then the previous hash map of the key value pair will be replaced. A hash map automatically increases capacity when it requires more storage space. For example, if the load factor of a hash map is 0.75, then when the capacity of the hash map is used by 75%, it increases the capacity to twice times the original capacity. For both arrays and linked list data structures, if you want to find a particular element that they store without knowing its location, you need to access the element from scratch until a match is found, and if the data structure contains many elements, it wastes time. It is better to use a hash map to store the data you are looking for, and use a hash map to reduce the cost of the retrieval.

1, hashmap<k, v> object

Hashmap<k, the object created by the V> generic class is called a hash map. For example:

Hashmap<string, student> hashtable = hashmap<string, student> ();

Then, Hashtable can store key-value pairs of data, where the key must be a string object, and the key corresponding to the value must be a student object. Hashtable can call the public V put (K key, V value) method to place the key value on the data store in the hash map, and return the value corresponding to the key.

2. Common methods

Hashmap<k, the common method for v> generic classes is as follows.

①public void Clear ()--Empty hash map

②public Object Clone ()--Returns a clone of the current hash map

③public boolean containskey (Object key)-the method returns true if the hash map has a key-value pair using the key specified by the parameter, otherwise it returns false

④public boolean containsvalue (Object value)--returns True if the hash map has a value for a key-value pair that is the value specified by the parameter, otherwise false

⑤public V get (Object key)-Returns the value of the key-value pair used as key in the hash map

⑥public Boolean IsEmpty ()--if the hash map does not contain any key-value pairs, the method returns true, otherwise false

⑦public V Remove (Object key)--Deletes the key-value pair specified by the key parameter in the hash map and returns the value corresponding to the key

⑧public int Size ()--Returns the size of the hash map, that is, the number of key-value pairs in the hash map

3. Traverse Hash Map

If you want to get the values from all the key-value pairs in the hash map, first use the

Public collection<v> VALUES ()

The Collection<v> method returns a reference to an object that implements the creation of an interface class and requires that a reference to the object be returned to the Collection<v> interface variable. The values returned by the values () method store the value in all the key-value pairs in the hash map, so that the interface variable can invoke the method implemented by the class, such as getting the iterator object, and then outputting all the values.

Example: Using a common method of hash mapping and traversing the hash map

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", "basic C + + tutorial"); Book Book2 = new book ("7808315162", "Java programming language"); Book BOOK3 = new book ("7302054991", "J2ME 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 + "in Stock");} Book B = Table.get ("7302054991"); System.out.println ("title:" +b.name+ ", ISBN:" +B.ISBN "); int number = Table.size (); System.out.println ("+number+" elements in the hash map: "); collection<book> Collection = table.values ();iterator<book> iter = Collection.iterator (); while ( Iter.hasnext ()) {Book Te = Iter.next (); System.out.printf ("title:%s, ISBN:%s\n", Te.name, TE. ISBN);}}}



The program runs the following results

Java programming language in stock
Title: J2ME Wireless Device Programming, isbn:7302054991
There are 3 elements in a hash map:
Title: Basic C + + tutorial, isbn:7302033218
Title: J2ME Wireless Device Programming, isbn:7302054991
Title: Java programming language, isbn:7808315162

4, hashmap<e> generic class implementation of the interface

The hashmap<e> generic class implements the generic interface Map<e>, and most of the methods are implemented Map<e> interface methods. When programming, you can use the interface callback technique, that is, to assign a reference to the Hashmap<e> object to the Map<e> interface variable, then the interface can invoke the interface method implemented by the class.

Hashmap<k, v> generic class

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.