关于java的map顺序的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011870280/article/details/85096684

java里面HashMap类的在put的时候会先hash下key算出数组的下标,hash的源码如下:

static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

可以看是根据key的hashCode得出来的hash值作为数组的下标,是按hash值的大小来排序的,跟key的值没有半点关联性,所以我们常说HashMap存的数据是无序的。

(顺便说下因为散列不均匀的问题,很多元素算出的hash值是一致的,造成链的长度增加,查询不方便。在JDK8已经HashMap由数组+链表改成红黑树结构)

如果按添加顺序可以用LinkedHashMap,key是按存在链表里,后来的元素会添加到链尾,与hash值大小无关。

Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
        LinkedHashMap.Entry<K,V> p =
            new LinkedHashMap.Entry<K,V>(hash, key, value, e);
        linkNodeLast(p);
        return p;
    }

如果想按key的值排序可以用TreeMap,如果没有设置比较器会调用key的compareTo方法来排序。

Comparator<? super K> cpr = comparator;
if (cpr != null) {
	do {
		parent = t;
		cmp = cpr.compare(key, t.key);
		if (cmp < 0)
			t = t.left;
		else if (cmp > 0)
			t = t.right;
		else
			return t.setValue(value);
	} while (t != null);
}
else {
	if (key == null)
		throw new NullPointerException();
	@SuppressWarnings("unchecked")
		Comparable<? super K> k = (Comparable<? super K>) key;
	do {
		parent = t;
		cmp = k.compareTo(t.key);
		if (cmp < 0)
			t = t.left;
		else if (cmp > 0)
			t = t.right;
		else
			return t.setValue(value);
	} while (t != null);
}

猜你喜欢

转载自blog.csdn.net/u011870280/article/details/85096684
今日推荐