Hashmap put(),是否总是排序? [英] Hashmap put(), is it always ordering?
问题描述
当我们使用put()
将值添加到hashmap<Key, Value>
变量时,它们是否始终排序?
When we add values to hashmap<Key, Value>
variable using put()
, are they always ordering?
因为当我尝试使用简单代码时,它们正在排序.
Because when I tried with simple codes they are ordering.
示例:
Map<Integer, Integer> ctrArMap = new HashMap<Integer, Integer>();
ctrArMap.put( 1, 11);
ctrArMap.put( 2, 12);
ctrArMap.put( 3, 13);
ctrArMap.put( 4, 14);
ctrArMap.put( 5, 15);
System.out.println(ctrArMap);
但就我而言,他们没有订购.
But in my case they aren't ordering.
推荐答案
-
HashMap :-
HashMap
永远不会保留您的广告订单.它在内部使用散列概念,通过该概念它为对应的key
生成HashCode
并将其添加到HashMap
.
HashMap :-
HashMap
never preserves your Insertion Order. It Internally Use a hashing Concept by which it generate aHashCode
to the Correspondingkey
and add it to theHashMap
.
LinkedHashMap :-LinkedHashMap
它保留您的广告订单.和keys
的顺序与您插入此LinkedHashMap
的顺序相同.
LinkedHashMap :- LinkedHashMap
It preserves your Insertion Order. and keys
will be found as same order you Insert into this LinkedHashMap
.
TreeMap :-TreeMap
类通过使用 Tree 实现Map
接口. TreeMap
提供了一种有效的方式来按排序顺序存储键/值对,并允许快速检索.
TreeMap :- The TreeMap
class implements the Map
interface by using a Tree. A TreeMap
provides an efficient means of storing key/value pairs in sorted order, and allows rapid retrieval.
您应注意,与HashMap
不同,树形图保证其元素将按升序排列.
You should note that, unlike a HashMap
, a tree map guarantees that its elements will be sorted in ascending key order
这篇关于Hashmap put(),是否总是排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!