Usage of map in Java (Haspmap usage)

Source: Internet
Author: User

Map<k,v>

The object that maps the key to a value. A map cannot contain duplicate keys, and each key can be mapped to at most one value.

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4 5  Public classTest1 {6 7      Public Static voidMain (string[] args) {8Map map =NewHashMap ();//declaring a map9Map.put ("s", "haha");//put values in map: Map is stored in the form of Key-valueTenString str = map.get ("s"). ToString ();//map Gets the value of key "s" One System.out.println (str); A     } -}

Output: haha

Map key-value pairs, where values are typically stored as objects.

HashMap common method, put (object Key,object value); Associates the specified value with the specified key in this map

Get (object key);//Find the corresponding value value based on the key value.

Determine if key exists: ContainsKey (object key)

Determine if the value exists: Containsvalue (object value)

The attribute of map is "key-value" (Key-value) match

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4 5 6  Public classTest1 {7 8      Public Static voidMain (string[] args) {9Map map =NewHashMap ();TenString key1 = "JAVA1"; OneString Key2 = "Java2"; AMap.put (Key1, "value of JAVA1"); -Map.put (Key2, "value of Java2"); - System.out.println (Map.get (Key1)); the System.out.println (Map.get (Key2)); -     } -}

Output:

The value of the JAVA1
The value of the JAVA2

Usage of map in Java (Haspmap usage)

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.