为什么 Sun Java 中的 HashSet 实现使用 HashMap 作为其

Why does HashSet implementation in Sun Java use HashMap as its backing?(为什么 Sun Java 中的 HashSet 实现使用 HashMap 作为其后盾?)

本文介绍了为什么 Sun Java 中的 HashSet 实现使用 HashMap 作为其后盾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看Java 6的源码,HashSet实际上是使用HashMap实现的,在Set的每个条目上使用虚拟对象实例.

Looking at the source of Java 6, HashSet<E> is actually implemented using HashMap<E,Object>, using dummy object instance on every entry of the Set.

我认为条目本身的大小会浪费 4 个字节(在 32 位机器上).

I think that wastes 4 byte (on 32-bit machines) for the size of the entry itself.

但是,为什么还要使用呢?除了让代码更容易维护之外,还有什么理由使用它吗?

But, why is it still used? Is there any reason to use it besides making it easier to maintain the codes?

推荐答案

其实不只是HashSet.在Java 6 中Set 接口的所有 实现都基于底层Map.这不是要求;这就是实现的方式.您可以通过查看 设置.

Actually, it's not just HashSet. All implementations of the Set interface in Java 6 are based on an underlying Map. This is not a requirement; it's just the way the implementation is. You can see for yourself by checking out the documentation for the various implementations of Set.

您的主要问题是

但是,为什么还要使用呢?在那儿除了制作它之外,还有任何使用它的理由更容易维护代码?

But, why is it still used? Is there any reason to use it besides making it easier to maintain the codes?

我认为代码维护是一个很大的激励因素.防止重复和膨胀也是如此.

I assume that code maintenance is a big motivating factor. So is preventing duplication and bloat.

SetMap 是相似的接口,不允许重复元素.(我认为 Map 支持的唯一 Set notCopyOnWriteArraySet,这是一个不寻常的 Collection,因为它是不可变.)

Set and Map are similar interfaces, in that duplicate elements are not allowed. (I think the only Set not backed by a Map is CopyOnWriteArraySet, which is an unusual Collection, because it's immutable.)

具体来说:

来自 文档设置:

一个集合不包含重复元素.更正式地说,集合不包含元素对 e1和 e2 使得 e1.equals(e2),并且在最多一个空元素.正如所暗示的那样它的名字,这个接口模拟了数学集合抽象.

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Set 界面放置了额外的规定,超出那些继承从 Collection 界面,在所有施工人员的合同以及add、equals 和哈希码方法.声明为其他继承的方法也是为方便起见,包括在此处.(这伴随这些规格声明已针对设置接口,但不包含任何附加规定.)

The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods. Declarations for other inherited methods are also included here for convenience. (The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations.)

附加规定构造函数,毫不奇怪,所有构造函数都必须创建一个不包含重复的集合元素(如上定义).

The additional stipulation on constructors is, not surprisingly, that all constructors must create a set that contains no duplicate elements (as defined above).

地图:

将键映射到值的对象.地图不能包含重复的键;每个键最多可以映射到一个值.

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

如果您可以使用现有代码来实现您的 Set,那么您可以从现有代码中实现的任何好处(例如速度)也会对您的 Set 产生影响.

If you can implement your Sets using existing code, any benefit (speed, for example) you can realize from existing code accrues to your Set as well.

如果您选择在没有 Map 支持的情况下实现 Set,则必须复制旨在防止重复元素的代码.啊,可口的讽刺.

If you choose to implement a Set without a Map backing, you have to duplicate code designed to prevent duplicate elements. Ah, the delicious irony.

也就是说,没有什么可以阻止您以不同的方式实现 Set.

That said, there's nothing preventing you from implementing your Sets differently.

这篇关于为什么 Sun Java 中的 HashSet 实现使用 HashMap 作为其后盾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么 Sun Java 中的 HashSet 实现使用 HashMap 作为其

基础教程推荐