Modify the value of map hashmap sorting iteration Loop

Source: Internet
Author: User
HashMap dgzhMap = Dict.getDict("dgzh");Iterator it_d = dgzhMap.entrySet().iterator();        while (it_d.hasNext()) {            Map.Entry entry_d = (Map.Entry) it_d.next();            Object key = entry_d.getKey();            Object value = entry_d.getValue();        value = value.toString().split("-")[0];        dgzhMap.put(key, value);    }

 

Sorting time of map (hashmap, treemap, hashtable, etc.) in Java
First, let's briefly talk about the differences between them:

Hashmap: the most commonly used map. It stores data based on the hashcode value of the key, and can directly obtain its value based on the key, with fast access speed. Hashmap allows a maximum of null keys for one record (multiple keys will overwrite). The value of multiple records is null. Non
First, let's briefly talk about the differences between them:

Hashmap: the most commonly used map. It stores data based on the hashcode value of the key, and can directly obtain its value based on the key, with fast access speed. Hashmap allows a maximum of null keys for one record (multiple keys will overwrite). The value of multiple records is null. Non-synchronous.

Treemap: sorts records stored by key. By default, the records are sorted in ascending order. You can also specify a comparator. When you use iterator to traverse treemap, the obtained records are sorted in ascending order. Treemap does not allow null key values. Non-synchronous.

Hashtable: similar to hashmap, the difference is that the values of key and value cannot be null. It supports thread synchronization, that is, only one thread can write hashtable at any time, as a result, hashtale writes slowly.

Linkedhashmap: stores the record insertion sequence. When iterator is used to traverse linkedhashmap, the first record must be inserted first. It will be slower than hashmap during traversal. Both the key and value values can be empty and non-synchronous.


Treemap performs sort by key in ascending order by default. To change the default order, you can use the comparator:

Map <string, string> map = new treemap <string, string> (new comparator <string> (){
Public int compare (string obj1, string obj2 ){
// Sort in descending order
Return obj2.compareto (obj1 );
}
});
Map. Put ("month", "The Month ");
Map. Put ("Bread", "the bread ");
Map. Put ("attack", "the attack ");

Set <string> keyset = map. keyset ();
Iterator <string> iter = keyset. iterator ();
While (ITER. hasnext ()){
String key = ITER. Next ();
System. Out. println (Key + ":" + map. Get (key ));
}


If you want to sort treemap by value, or sort hashmap, hashtable, and linkedhashmap, you can use the map. Entry <K, V> interface in combination with list:

Eg.1 for treemap in ascending order of value values:

List <map. Entry <string, string> mappinglist = NULL;
Map <string, string> map = new treemap <string, string> ();
Map. Put ("aaaa", "month ");
Map. Put ("BBBB", "Bread ");
Map. Put ("CCCCC", "attack ");

// Use the arraylist constructor to convert map. entryset () to list.
Mappinglist = new arraylist <map. Entry <string, string> (Map. entryset ());
// Compare and sort by comparator
Collections. Sort (mappinglist, new comparator <map. Entry <string, string> (){
Public int compare (Map. Entry <string, string> mapping1, map. Entry <string, string> mapping2 ){
Return mapping1.getvalue (). compareto (mapping2.getvalue ());
}
});

For (Map. Entry <string, string> mapping: mappinglist ){
System. Out. println (mapping. getkey () + ":" + mapping. getvalue ());
}


In eg.2, hashmap (or hashtable, linkedhashmap) is sorted in ascending order of key values:

List <map. Entry <string, string> mappinglist = NULL;
Map <string, string> map = new hashmap <string, string> ();
Map. Put ("month", "month ");
Map. Put ("Bread", "Bread ");
Map. Put ("attack", "attack ");

// Use the arraylist constructor to convert map. entryset () to list.
Mappinglist = new arraylist <map. Entry <string, string> (Map. entryset ());
// Compare and sort by comparator
Collections. Sort (mappinglist, new comparator <map. Entry <string, string> (){
Public int compare (Map. Entry <string, string> mapping1, map. Entry <string, string> mapping2 ){
Return mapping1.getkey (). compareto (mapping2.getkey ());
}
});

 

When it comes to traversal, we should first think of a for loop. However, the map setOfTraversal is usually like this inOfTo obtain an iterator.


[Java]
Map <integer, string> map = newHashmap<> ();

Iterator it = map. entryset (). iterator ();

While (it. hasnext ()){

Map. Entry entry = (Map. Entry) it. Next ();

Object key = entry. getkey ();

ObjectValue= Entry. getvalue ();

Map <integer, string> map = newHashmap<> ();
 
Iterator it = map. entryset (). iterator ();
 
While (it. hasnext ()){
 
Map. Entry entry = (Map. Entry) it. Next ();
 
Object key = entry. getkey ();
 
ObjectValue= Entry. getvalue ();
In fact, a foreach loop is also acceptable.OfIt's very concise ~


[Java]
For (Map. Entry <integer, integer> M: map. entryset ())
{
If (ARR [I] = (INT) M. getkey ())
Map. Put (INT) M. getkey (), (INT) M. getvalue () + 1 );
}

For (Map. Entry <integer, integer> M: map. entryset ())
{
If (ARR [I] = (INT) M. getkey ())
Map. Put (INT) M. getkey (), (INT) M. getvalue () + 1 );
}
Attach a completeOfSmall program example.

The random generation length is 100.OfArray. The array element ranges from 1 to 10, and the maximum and minimum number of occurrences are counted.OfElement

 

[Java]
MportJava. Util .*;
Class count
{
Public void count (INT [] ARR)
{
Int num = 0;
Map <integer, integer> map = newHashmap<Integer, integer> ();
For (INT I = 1; I <= 10; I ++)
{
Map. Put (I, num );
}
For (INT I = 0; I <arr. length; I ++)
{
/* Iterator it = map. entryset (). iterator ();
While (it. hasnext ())
{
Map. Entry M = (Map. Entry) it. Next ();
If (ARR [I] = (INT) M. getkey ())
Map. Put (INT) M. getkey (), (INT) M. getvalue () + 1 );
}*/
For (Map. Entry <integer, integer> M: map. entryset ())
{
If (ARR [I] = (INT) M. getkey ())
Map. Put (INT) M. getkey (), (INT) M. getvalue () + 1 );
}
}
For (Map. Entry <integer, integer> M: map. entryset ())
{
System. Out. println ("" + M. getkey () + "appearsOfNumber of times: "+ M. getvalue () +" times ");
}
}

Public static void main (string [] ARGs)
{
Random RD = new random ();
Int [] arr = new int [100];
For (INT I = 0; I <100; I ++)
{
Arr [I] = RD. nextint (10) + 1;
}
New count (). Count (ARR );
}
}

ImportJava. Util .*;
Class count
{
Public void count (INT [] ARR)
{
Int num = 0;
Map <integer, integer> map = newHashmap<Integer, integer> ();
For (INT I = 1; I <= 10; I ++)
{
Map. Put (I, num );
}
For (INT I = 0; I <arr. length; I ++)
{
/* Iterator it = map. entryset (). iterator ();
While (it. hasnext ())
{
Map. Entry M = (Map. Entry) it. Next ();
If (ARR [I] = (INT) M. getkey ())
Map. Put (INT) M. getkey (), (INT) M. getvalue () + 1 );
}*/
For (Map. Entry <integer, integer> M: map. entryset ())
{
If (ARR [I] = (INT) M. getkey ())
Map. Put (INT) M. getkey (), (INT) M. getvalue () + 1 );
}
}
For (Map. Entry <integer, integer> M: map. entryset ())
{
System. Out. println ("" + M. getkey () + "appearsOfNumber of times: "+ M. getvalue () +" times ");
}
}
 
Public static void main (string [] ARGs)
{
Random RD = new random ();
Int [] arr = new int [100];
For (INT I = 0; I <100; I ++)
{
Arr [I] = RD. nextint (10) + 1;
}
New count (). Count (ARR );
}
}



For (Map. Entry <string, string> mapping: mappinglist ){
System. Out. println (mapping. getkey () + ":" + mapping. getvalue ());
}

Modify the value of map hashmap sorting iteration Loop

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.