HashMap,TreeMap以及LinkedHashMap的区别

刚才看帖子发现有个人说了下他的面试,其中有一段是关于HashMap的

问:HashMap是有序的么,回答无序,回答有序的同学请点下面

=======相关链接======

手写hashmap请点击雷霆崖传送门

问:那有没有有序的map,回答treemap和linkedhashmap

这里引出了这个问题,为什么这两个可以有序,我先给出一段网上随处可见的他们三者的区别

1.HashMap里面存入的键值对在取出的时候是随机的,也是我们最常用的一个Map.它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。在Map 中插入、删除和定位元素,HashMap 是最好的选择  
2.TreeMap取出来的是排序后的键值对。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好
3. LinkedHashMap 是HashMap的一个子类,如果需要输出的顺序和输入的相同,那么用LinkedHashMap可以实现

这么回答没问题,接下来会问他们是怎么实现的有序,这里可以通过数据结构想一下,tree的中序遍历是有序的,是不是treemap的迭代使用的中序遍历,并且是自然顺序,而链表是一个链一个的,所以输出的顺序是插入的顺序,这些就不得而知了,经过证实treemap里的entry是一颗红黑树,红黑树也是一颗平衡二叉树,比二叉树多了左旋,右旋以及反转这三个平衡方法

对于二叉树的了解可以点一下链接

=====相关链接=====

手写二叉树暴风城传送门

下面给出几段小代码,大家可以尝试下

public class Test{
	
	public static void main(String[] args) {
		try {
			useHashMap();
			System.out.println();
			useTreeMap();
			System.out.println();
			useLikedHashMap();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void useHashMap() throws Exception {      
		Map<String, String> map = new HashMap<String, String>();      
		map.put("1", "Level 1");      
		map.put("2", "Level 2");      
		map.put("3", "Level 3");      
		map.put("a", "Level a");      
		map.put("c", "Level c");      
		map.put("b", "Level b");  
		Iterator<Entry<String, String>> it = map.entrySet().iterator();      
		while (it.hasNext()) {       
			Entry<String, String> e = it.next();       
			System.out.println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
		}  
	}  
	
	public static void useTreeMap() throws Exception {         
        Map<String, String> map = new TreeMap<String, String>();      
        map.put("1", "Level 1");      
        map.put("3", "Level 3");      
        map.put("2", "Level 2");      
        map.put("a", "Level a");      
        map.put("c", "Level c");      
        map.put("b", "Level b");      
        Iterator<Entry<String, String>> it = map.entrySet().iterator();      
        while (it.hasNext()) {       
            Entry<String, String> e = it.next();       
            System.out.println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
        }  
    }  
	
    public static void useLikedHashMap() throws Exception {      
        Map<String, String> map = new LinkedHashMap<String, String>();      
        map.put("1", "Level 1");      
        map.put("2", "Level 2");      
        map.put("3", "Level 3");      
        map.put("a", "Level a");      
        map.put("c", "Level c");      
        map.put("b", "Level b");  
        Iterator<Entry<String, String>> it = map.entrySet().iterator();      
        while (it.hasNext()) {       
            Entry<String, String> e = it.next();       
            System.out.println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
        }  
    }  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HashMapTreeMapLinkedHashMap是Java中的三种Map实现类。HashMap是无序的,LinkedHashMap是按照插入顺序排序,而TreeMap是根据键的值进行排序后再进行插入。 HashMap是通过哈希表实现的,它使用键值对存储数据,可以快速地进行插入、删除和查找操作。HashMap的插入顺序是无序的,因为它不会记录插入的顺序。 LinkedHashMap也是通过哈希表实现的,但它还使用一个双向链表来维护插入顺序。因此,LinkedHashMap可以按照插入的顺序进行遍历。 TreeMap是通过红黑树实现的,它会根据键的值进行排序后再进行插入。因此,TreeMap的遍历顺序是按照键的排序顺序来的。如果你需要对Map对象进行排序,可以使用TreeMap。 在运行方面,HashMapLinkedHashMap的运行速度较快,适合存储大量数据。而TreeMap的运行效率较低,适合对Map对象进行排序的场景。 需要注意的是,在遍历时,LinkedHashMap通常比HashMap快,因为LinkedHashMap的遍历速度只和实际数据有关,和容量无关。而HashMap的遍历速度和容量有关。 综上所述,HashMapTreeMapLinkedHashMap在插入顺序、排序方式和运行效率方面有所不同,根据具体需求可以选择适合的实现类。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [HashMapLinkedHashMapTreeMap区别](https://blog.csdn.net/an341221/article/details/79279938)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Java----Hashmap,LinkedMap和TreeMap三者的区别](https://blog.csdn.net/weixin_65237252/article/details/128986118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [HashMapLinkedHashMapTreeMap](https://blog.csdn.net/Lxcjl/article/details/126755089)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值