Java---Map interface implementation class

Source: Internet
Author: User

Map is a two-column set interface, if the map interface is implemented, the characteristic is that the data is in the form of key-value pairs, the keys cannot be duplicated, and the values can be duplicated. Java mainly has HashMap, TreeMap, Hashtable. This article mainly introduces the interface method of map:

1 HashMap, TreeMap, Hashtable

Storage principle of HashMap:

The HASHMAP is also based on a hash table. When adding elements to HashMap, the Hashcode method of the key is called first to obtain the hash code value of the element, and then the operation can calculate the location of the element in the hash table.

Scenario 1: If the calculated position does not currently have any element storage, then the element can be added directly to the hash table.

Case 2: If the calculated position currently has other elements, then the Equals method of the element is also called to compare with the element at this location, if the Equals method returns False, then the element is allowed to be stored, if the Equals method returns True, Then the element is treated as a repeating element, forbid storage.

TreeMap Storage principle:

TreeMap is also based on the red-black tree (binary tree) data structure implementation, characteristics: The key of the element will be sorted storage.

TreeMap things to be aware of:
1. When adding elements to TreeMap, if the keys of the elements are in a natural order, they are sorted by the natural order characteristics of the keys.
2. When adding elements to TreeMap, if the key of the element does not have a natural order attribute, then the class that the key belongs to must implement the comparable interface, and the key comparison rule is defined on the CompareTo method.
3. When adding elements to TreeMap, if the key of the element does not have a natural order attribute, and the class to which the key belongs does not implement the comparable interface, then it is necessary to pass in the comparer when creating the TreeMap object.

Hashtable Storage principle:

The bottom is also the hash table realization, the realization way and hashmap consistent, but Hashtable is thread-safe, the operation efficiency is low.

Http://www.cnblogs.com/K-artorias/p/7090314.html link in, the article tells the difference between HashMap and Hashtable.

2 Method of Map interface:
Add to:
Put (K key, V value)
Putall (map<? extends K,? extends v> m)
Delete
Remove (Object key)
Clear ()

Get:
Get (Object key)
Size ()
Judge:
ContainsKey (Object key)
Containsvalue (Object value)
IsEmpty ()

The following is an example of code for the above interface methods:

1  Public classDemo2 {2     3      Public Static voidMain (string[] args) {4map<string,string> map =NewHashmap<string, string>();5         //Add Method6Map.put ("A", "a");7Map.put ("B", "B");8Map.put ("C", "C");9         Ten         /* One Add A System.out.println ("Return Value:" +map.put ("A", "@"); If the key is not previously present, then NULL is returned, and if the key has already existed before, then the value corresponding to the key is returned.  - map<string,string> map2 = new hashmap<string, string> (); - map2.put ("M", "M"); the Map2.put ("n", "n"); - Map.putall (MAP2);//Add MAP2 elements to the map collection.  -          -         */ +          -         /* + Delete A System.out.println ("Deleted data is:" +map.remove ("a")); Deletes the data from a map by key, and returns the value corresponding to the key.  at map.clear ();//Clears all data in the collection.  -         */ -          -         /*Get - System.out.println ("gets the corresponding value according to the specified key:" + map.get ("B")); - System.out.println ("Get the number of map collection key pairs:" +map.size ()); in          -          to Judging + System.out.println ("Determines whether the map collection contains the specified key:" + Map.containskey ("B")); - System.out.println ("Determine if the map collection contains the specified value:" + map.containsvalue ("C")); the map.clear (); * System.out.println ("Determine if the map set is an empty element:" + map.isempty ()); $         */Panax NotoginsengSystem.out.println ("Elements of the collection:" +map); -          the          +     } A  the}

Java---Map interface implementation 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.