Hashmap put(),是否总是排序? [英] Hashmap put(), is it always ordering?

查看:369
本文介绍了Hashmap put(),是否总是排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用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.

推荐答案

  1. HashMap :-HashMap永远不会保留您的广告订单.它在内部使用散列概念,通过该概念它为对应的key生成HashCode并将其添加到HashMap.

  1. HashMap :- HashMap never preserves your Insertion Order. It Internally Use a hashing Concept by which it generate a HashCode to the Corresponding key and add it to the HashMap.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆