Detailed description of map usage in Java

Source: Internet
Author: User

Map to press / The data is stored in the form of a numeric pair , which is specifically described here ( map.entry, which is The inner class of the map, which is used to describe the map the key / value pairs in the Map is an interface, we usually use its implementation class HashMap.

The use cases are as follows:

  Public     static     void     Main (String     args[])       {           HashMap     HashMap     =     new     HashMap ();           Hashmap.put ("Item0",     "Value0");           Hashmap.put ("Item1",     "Value1");           Hashmap.put ("Item2",     "Value2");           Hashmap.put ("Item3",     "Value3");           Set     Set     =     hashmap.entryset ();           Iterator     Iterator     =     set.iterator ();           While     (Iterator.hasnext ()         {             map.entry     mapentry     =     (map.entry)     Iterator.next ( );             System.out.println (Mapentry.getkey ()     +     "/"     +     Mapentry.getvalue ());         }       }   

Note that the keys to this Map must be unique, such as the inability to have two keys null. of course, you can do that.

map<string, order> map = new hashmap<string, order> ();            Map.put ("Order", (order) obj);

Detailed Understanding:

       Collection container contains set list interface, set list linkedlist and arraylist map interface only hashmap

Java.utilthe collection class in containsJavaSome of the most commonly used classes in the. The most common collection classes areListand theMap. Listthe specific implementation includesArrayListand theVector, they are a variable-sized list of elements that are appropriate for building, storing, and manipulating any type of object. Listfor cases where an element is accessed by a numeric index, the dataSequential and repeatable (provided in orderGet(I) (the index method). andSetthe data is not sequential and cannot be duplicated.

Table 1 : we put this Object the two methods are overwritten to correctly compare Map the equivalence of objects.

Equals (Object o)

compares the specified object with this Map the equivalence of

Hashcode ()

return this Map the hash code


Table 2: map Update method: You can change the map content.

Clear ()

from Map Remove all mappings in

Remove (Object key)

from Map Delete keys and associated values in the

Put (object key, Object value)

Associate a specified value with a specified key

Clear ()

from Map Remove all mappings in

Putall (Map t)

will specify Map Copy all the mappings in the Map

Key points: Traverse Map

       iteration Map If you want to query a Map Span style= "Color:rgb (51,51,51)", "view" . The

3 : Return to the view of Map Span style= "Color:rgb (51,51,51)" > method. by using the objects returned by these methods, you can traverse the Map Map

entryset ()

returns map The Set view. Set Each element is a Map.entry object, you can use the GetKey () GetValue () method (there is also a SetValue () method) accesses the key element and value element of the latter

keyset ()

returns Map Set view. delete Set The elements in the map

VALUES ()

return Map contains the values in the Collection view. deleting The elements in Collection also deletes the corresponding mappings (keys and values) in the map

all key-value pairs - See EntrySet ()

All keys - See KeySet ()

All values     - see values ()

The first two views return a Set object, and the third view returns the Collection object. in both cases, the problem does not end here because you cannot iterate directly over the Collection object or the Set object. To iterate, you must obtain a Iterator object. Therefore, It is relatively troublesome to iterate over the elements of Map, but there is no better way.

Iteratorkeyvaluepairs = Amap.entryset (). iterator (); iterator keys= Amap.keyset (). iterator (); iteratorvalues = Amap.values (). iterator ();

Table 4 : Map Access and test methods: These methods retrieve information about Map content without changing the information Map content.

Get (Object key)

Returns the value associated with the specified key

ContainsKey (Object key)

if Map contains the mappings for the specified key, the return true

Containsvalue (Object value)

if this Map maps One or more keys to a specified value, returns true

IsEmpty ()

if Map does not contain a key - value mapping, the return true

Size ()

return Map the keys in - number of value mappings

Write your own use case code as follows:

Package Mapusage;import Java.util.collection;import Java.util.hashmap;import java.util.iterator;import Java.util.map;import Java.util.map.entry;import Java.util.set;public class Guo {private Map Map;guo () {//map< integer,string> map =new hashmap<integer,string> () this.map =new HashMap (); Map.put (1, "str1"); Map.put (2, " Str2 "); Map.put (3," str1 ");} public static void Main (string[] args) {//TODO auto-generated method Stubguo Guo =new guo (); guo.fun1 (); guo.fun2 ();} Public <E> void Fun1 () {//Traversal MapSystem.out.println (Map.size ());//map map number System.out.println (Map.entryset ().  GetClass ()); HashMap's inner class entryset//map three views Set<entry<integer, String>> set =map.entryset ();//set<integer> Set_key =map.keyset (); Set<e> Set_key = (set<e>) map.keyset ();  collection<string> Col_value = Map.values (); Cannot return set,values value may have duplicate Iterator<map.entry<integer, string>> itera_entry =set.iterator (); iterator Itera_ Key =set_key.iterator ();iterator<string> ITERA_value =col_value.iterator ();//iterator while (Itera_entry.hasnext ()) {Map.entry<integer, string> mapEntry =itera_ Entry.next (); System.out.println ("Key:" +mapentry.getkey () + "value:" +mapentry.getvalue ());} Enhanced for Loop/*for (Integer key:set_key) {System.out.println ("key:" +key);} */for (E key:set_key) {System.out.println ("key:" +key);} for (String value:col_value) {System.out.println ("value:" +value);}} Public Boolean fun2 () {Boolean b;b =map.containskey (5); System.out.println (b); return b;}}


Detailed description of map usage in Java

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.