HashSet 和 HashMap 的区别? [英] Difference between HashSet and HashMap?

查看:30
本文介绍了HashSet 和 HashMap 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了HashSet不允许重复值,HashMapHashSet还有什么区别?

Apart from the fact that HashSet does not allow duplicate values, what is the difference between HashMap and HashSet?

我的意思是实施明智?这有点含糊,因为两者都使用哈希表来存储值.

I mean implementation wise? It's a little bit vague because both use hash tables to store values.

推荐答案

它们是完全不同的结构.HashMapMap 的实现.Map 将键映射到值.使用散列进行键查找.

They are entirely different constructs. A HashMap is an implementation of Map. A Map maps keys to values. The key look up occurs using the hash.

另一方面,HashSetSet 的实现.Set 旨在匹配数学模型一套.正如您所指出的,HashSet 确实使用 HashMap 来支持其实现.然而,它实现了一个完全不同的接口.

On the other hand, a HashSet is an implementation of Set. A Set is designed to match the mathematical model of a set. A HashSet does use a HashMap to back its implementation, as you noted. However, it implements an entirely different interface.

当您正在寻找最适合您的Collection 时,这个教程 是一个很好的起点.如果你真的想知道发生了什么,有一本书可以解决这个问题,也是.

When you are looking for what will be the best Collection for your purposes, this Tutorial is a good starting place. If you truly want to know what's going on, there's a book for that, too.

这篇关于HashSet 和 HashMap 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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