Sorting of the return value of keySet() in various maps in java

Keywords: Java

Last When it comes to data processing, the parallel threads occupy a part of the data, and no one is willing to release it, thus issuing a life and death lock.

The reason for the disorder is that the HashMap.keySet() method is used to get the primary key of the data row, and the data in the Set result returned by this method is in disorder.

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

package test920;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.TreeMap;

public class MapTest {

	public static void main(String[] args) {
		MapTest.hashtable();
		System.out.println("\n---------------------------------");
		MapTest.treeMap();
		System.out.println("\n---------------------------------");
		MapTest.hashMap();
		System.out.println("\n---------------------------------");
		MapTest.linkedHashMap();
	}

	public static void hashtable() {
		/////////////////////////////////////////////////////////////
		// 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.print(it.next() + "\t");
		}
	}

	public static void treeMap() {
		/////////////////////////////////////////////////////////////
		// 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> it = tm.keySet().iterator();
		while (it.hasNext()) {
			System.out.print(it.next() + "\t");
		}
	}

	public static void hashMap() {
		/////////////////////////////////////////////////////////////
		// TEST
		/////////////////////////////////////////////////////////////
		System.out.println("## HashMap ##");
		HashMap<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> it = hm.keySet().iterator();
		while (it.hasNext()) {
			System.out.print(it.next() + "\t");
		}
	}

	public static void linkedHashMap() {
		/////////////////////////////////////////////////////////////
		// 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> it = lhm.keySet().iterator();
		while (it.hasNext()) {
			System.out.print(it.next() + "\t");
		}
	}

}

Result:

## Hashtable ##
5    4    3    2    1    
---------------------------------
## TreeMap ##
1    2    3    4    5    
---------------------------------
## HashMap ##
1    2    3    4    5    
---------------------------------
## linkedHashMap ##
1    3    2    5    4

It can be seen from this that it is roughly as follows

Hashtable.keySet() descending

TreeMap.keySet() ascending

HashMap.keySet() out of order

LinkedHashMap.keySet() original order

 

Except for treemap. keySet(), the order of the return values of keySet() is not clearly stated in Javadoc,

In practical application, if there are clear requirements for the order, it is better to clear the order.

Posted by mistcat on Fri, 27 Dec 2019 09:34:54 -0800