Map. Entry

Source: Internet
Author: User
Tags map class set set

Example:

Public String getreporturl () throws exception {
String reporturl = parametermap. Get ("showpageurl"). tostring () + "showpagenum = 1 ";
Iterator it = parametermap. entryset (). iterator ();
While (it. hasnext ()){
Map. Entry entry = (Map. Entry) it. Next ();
Object keyobj = entry. getkey ();
Object valobj = entry. getvalue ();
If (keyobj! = NULL & keyobj. tostring (). Length ()> 0 & valobj! = NULL & valobj. tostring (). Length ()> 0 ){
String key = keyobj. tostring ();
String val = valobj. tostring ();
If (! "Showpagenum". Equals (key ))
Reporturl + = "&" + urlencoder. encode (key, "gb2312") + "=" + urlencoder. encode (Val, "gb2312 ");
}
}
Return reporturl;
}

 

The map class provides a method called entryset (), which returns an object set after map. Entry instantiation. Next, the map. Entry class provides a getkey () method and a getvalue () method.

 

At the same time, it provides developers with a class that maintains both the keyword and its corresponding value. Map. Entry also provides a setvalue () method, which can be used by programmers to modify values in map.

The map is stored by the hash algorithm, but it is useful to sort the map in some cases.

/**
* @ Param H
* @ Return
* Sort the MAP values in ascending order.
*/
@ Suppresswarnings ("unchecked ")
Public static map. Entry [] getsortedhashtablebyvalue (MAP h ){
Set set = H. entryset ();
Map. Entry [] entries = (Map. Entry []) set. toarray (new map. Entry [set. Size ()]);
Arrays. Sort (entries, new comparator (){
Public int compare (Object arg0, object arg1 ){
Long key1 = long. valueof (Map. Entry) arg0). getvalue (). tostring ());
Long key2 = long. valueof (Map. Entry) arg1). getvalue (). tostring ());
Return key1.compareto (key2 );
}
});

Return entries;
}

/**
* @ Param H
* @ Return
* Sort map by key
*/
@ Suppresswarnings ("unchecked ")
Public static map. Entry [] getsortedhashtablebykey (MAP h ){

Set set = H. entryset ();

Map. Entry [] entries = (Map. Entry []) set. toarray (new map. Entry [set. Size ()]);

Arrays. Sort (entries, new comparator (){
Public int compare (Object arg0, object arg1 ){
Object key1 = (Map. Entry) arg0). getkey ();
Object key2 = (Map. Entry) arg1). getkey ();
Return (comparable) key1). compareto (key2 );
}

});

Return entries;
}

 

 

 

Map keyset Method

 

There is a map object. In this case, the keyset () method is used to obtain all key values, for example:

Map map = new hashmap ();

Map. Put (1, "");

Map. Put (2, "B ");

Map. Put (3, "C ");

Map. Put (4, "D ");

Set keys1 = map. keyset ();

Set keys2 = map. keyset ();

Set keys3 = map. keyset ();

The preceding three set objects key1, key2, and key3 reference an object. This is map's keyset () method. Only one set instance is returned. Therefore, when an object is deleted from key1, key2 and key3 will be affected.

Keys1.remove (1 );

System. Out. println (keys1 );

System. Out. println (keys2 );

System. Out. println (keys3 );

The output is as follows:

[2, 4, 3]

[2, 4, 3]

[2, 4, 3]

 

This type of error occurs sometimes when map is used.

Java. util. concurrentmodificationexception

 

This error occurs when the following code is executed:

Set <string> keyset = map. keyset ();

For (string key: keyset ){

Map. Remove (key );

}

The reason is that the keyset set is incorrect after an entry is deleted, and an error occurs during loop execution. This problem can be solved by changing the code to the following:

String [] keyset = map. keyset (). toarray (New String [0])

For (string key: keyset ){

Map. Remove (key );

}

 

Because toarray is added, the new set has no relationship with the original keyset, so there will be no errors.

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.