Java Basics (eight)---map

Source: Internet
Author: User
Tags set set

MAP

Map is used to hold data for a specific mapping, so there are two sets of values stored in the map collection, one set of values to hold the key in the map, and the other set of values to hold the Value,key and value in the map can be any type of data. The key of map does not allow repetition, that is, any two key of the same map object is always returned false by the Equals method.

The map interface defines the following methods:

V put (K key,v value)    --------------------> Add a key-value pair, if there is already a key-value pair that is equal to the current key in the current map, The new key-value will overwrite the original Key-value pair. V Remove (Object key)  ---------------------> delete the key-value pair corresponding to the specified key, return the value associated with the deleted key, if key does not exist return nullvoid Clear ()                 ---------------------> Deletes all key-value in the collection to the Boolean ContainsKey (Object key)-----------> Queries whether the collection contains the specified key if it contains a return Trueboolean Containsvalue (Object value)-------> Query whether the collection contains one or more value, if included returns Trueboolean IsEmpty ()        ---------------- ----> Query whether the collection is empty int size ()                      ------------------ --Query the number of Key-value pairs in the collection set EntrySet ()              -------------------> Returns the set set of Key-value pairs contained in the collection, each of which is map.entry (entry is the inner class of the map) object Set keyset ()                -------------------> Returns a set set of all the keys in the map   expansion: Map contains an inner class ENTRy, this class encapsulates a key-value pair.  Entry contains the following three methods: Object GetKey (): Returns the key value contained in the entry.  Object getValue (): Returns the value contained in the entry. Object SetValue (V value): Sets the value contained in the entry and returns the value of the new setting

Traversal of the Map collection:


Example:
Map<string,string> HM = new hashmap<string,string> ();

Hm.put ("it002", "Hello");
Hm.put ("it003", "World");
Hm.put ("it001", "Java");

A: Key to find the value
set<string> set = Hm.keyset ();//Gets the collection of all keys
for (String Key:set) {
String value = Hm.get (key);
System.out.println (key+ "---" +value);
}

B: Key-value pair object find key and value
Set<map.entry<string,string>> Set2 = Hm.entryset ();//Gets the collection of all key-value pairs of objects
for (map.entry<string,string> Me:set2) {
String key = Me.getkey ();
String value = Me.getvalue ();
System.out.println (key+ "---" +value);

} Data structure

ARRAYXXX: The underlying data structure is an array, query fast, and delete slowly
LINKEDXXX: The underlying data structure is linked list, query slow, and delete quickly
HASHXXX: The underlying data structure is a hash table. Dependent on two methods: Hashcode () and Equals ()
TREEXXX: The underlying data structure is a two-fork tree. Sort by two ways: natural sort and comparator sort

The HashMap and Hashtable keys are hash table structures, which guarantee the uniqueness of the keys HashMap and Hashtable are the implementation classes of the map interface. Their relationship is exactly like the relationship between ArrayList and Vector: Hashtable is an ancient map implementation class that began to appear from JDK1.0, when Java did not provide a map interface. HashMap and Hashtable differences: 1.Hashtable is a thread-safe map implementation, but HashMap is a thread insecure implementation, so hashmap high performance 2.Hashtable does not allow the use of NULL as key and value, If you attempt to put Null into Hashtable, the nullpointexception exception is reported, but HashMap can be placed as a key or value.

A sub-category of Hashtable properties:

Properties similar to the INI file on the Windows operating platform is a property file. Properties can be used to manage the map object and the properties file so that the Key-value pairs in the map object can be written to the property file. You can also load the contents of the properties file into the Map object, because the attribute file property name, property value can only be a string type, so the properties of the key, value is a string type.

It has the following methods:

String GetProperty (String key): Gets the property value that corresponds to the property name specified in properties.

Object SetProperty (string key, String value): Sets the property value, similar to the Hashtable put method.

void load (Reader reader): Loads the Key-value pair from the properties file, appends the loaded Key-value pair to properties.

void Store (OutputStream out, String comments): Outputs the key-value of the properties to the property file.

The TreeMap key is a red-black tree structure that guarantees the ordering and uniqueness of keys

Java Basics (eight)---map

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.