The sorting problem of the return value of keySet() in Java

As mentioned last time, because the data is processed line by line out of order, the parallel threads each occupy a part of the data, and no one is willing to release it, resulting in a deadlock.

The reason why it is out of order is because the HashMap.keySet() method is used when the primary key of the data row is obtained, and the Set result returned by this method has the data in it arranged out of order.

There is no detailed explanation in JavaDoc, so I tried it with code

copy code
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Test2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        /////////////////////////////////////////////////////////////
        // TEST/////////////////////////////////////////////////////////////        System.out.println("##  Hashtable  ##");
        

        Hashtable<String , String> ht = new Hashtable<String , String>();
        ht.put("1", "OOO");
        ht.put("3", "OOO");
        ht.put("2", "OOO");
        ht.put("5", "OOO");
        ht.put("4", "OOO");
        
        Iterator<String> it =  ht.keySet().iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        
        /////////////////////////////////////////////////////////////
        // TEST/////////////////////////////////////////////////////////////        System.out.println("##  TreeMap  ##");
        

        TreeMap<String , String> tm = new TreeMap<String , String>();
        tm.put("1", "OOO");
        tm.put("3", "OOO");
        tm.put("2", "OOO");
        tm.put("5", "OOO");
        tm.put("4", "OOO");

        Iterator<String> it2 =  tm.keySet().iterator();


        while (it2.hasNext()) {
            System.out.println(it2.next());
        }
        
        /////////////////////////////////////////////////////////////
        // TEST/////////////////////////////////////////////////////////////        System.out.println("##  HashMap  ##");
        

        Map<String , String> hm = new HashMap<String , String>();
        hm.put("1", "OOO");
        hm.put("3", "OOO");
        hm.put("2", "OOO");
        hm.put("5", "OOO");
        hm.put("4", "OOO");

        Iterator<String> it3 =  hm.keySet().iterator();

        while (it3.hasNext()) {
            System.out.println(it3.next());
        }
        
        /////////////////////////////////////////////////////////////
        // TEST/////////////////////////////////////////////////////////////        System.out.println("##  LinkedHashMap  ##");
        

        LinkedHashMap<String, String> lhm = new LinkedHashMap<String , String>();
        lhm.put("1", "OOO");
        lhm.put("3", "OOO");
        lhm.put("2", "OOO");
        lhm.put("5", "OOO");
        lhm.put("4", "OOO");

        Iterator<String> it4 =  lhm.keySet().iterator();

        while (it4.hasNext()) {
            System.out.println(it4.next());
        }
    }

}
copy code

 

The output is as follows

copy code
##  Hashtable  ##
5
4
3
2
1
##  TreeMap  ##
1
2
3
4
5
##  HashMap  ##
3
2
1
5
4
## LinkedHashMap ##
1
3
2
5
4
copy code

It can be seen that roughly as follows

  Hashtable.keySet()          降序

  TreeMap.keySet() ascending order

  HashMap.keySet()            乱序

  LinkedHashMap.keySet()      原序


Except for TreeMap.keySet(), the order of return values ​​of keySet() is not clearly stated in the JavaDoc,

In practical applications, if there is a clear requirement for the order, it is best to clear the order.

 

--------------
The road of architects is a long way to go

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770745&siteId=291194637