- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在这个网站上学习Collection Framework:http://way2java.com/collections/hashtable-about/ 。在阅读了 Hashtable
的所有方法之后,我看到了两种访问表键的方法:
Set keys()
:返回包含所有键的Set
对象
Set keySet()
:返回一个包含Hashtable
所有键的Set
对象。一个相似之处是 Hashtable
和 Set
不允许重复。在 Set
中添加和删除元素也反射(reflect)在 Hashtable
它们都返回一个Set
对象。我看不出他们之间有什么不同。谁能告诉我这个吗?
最佳答案
keys()
不返回 Set
,它返回 Enumeration<K>
.
Hashtable
是一个非常遗留的类,不再推荐使用。它被替换为 HashMap
,或ConcurrentHashMap
†。它在 JCF 出现之前就已存在,因此一开始获取 key 的标准方法是通过 Enumeration
- 用于移动对象集合的原始 Java 接口(interface)。
然后是 Java 1.2,以及 JCF 。 Hashtable
已 retrofit 为 Map
与 keySet()
的接口(interface)返回 Set
的方法(也随 JCF 一起引入)。 keys
出于遗留兼容性原因保留了方法。 Set
从新方法返回实现了两件事:
Hashtable
的按键是一个数学集合Iterable<T>
,它取代 Enumerable<T>
† 来自 Hashtable
文档:
As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
关于java - Java Hashtable 中的 Set keys() 和 Set keyset() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32177557/
我正在学习 Java Map 接口(interface),并希望从 Map 的键值创建一个 Set 。我想出了两个版本的代码,它们对我来说似乎完全相同。我只是想知道它们之间是否有任何实际区别以及我什么
我有一个LinkedHahMap map1,其中的key作为我的beam_current,它是 double 类型,值作为我的logtime,它是字符串类型。现在我想在我的sql查询中使用这个ma
我正在用 Java 编写模型、 Controller 等。 我有一个数据模型,其中包含 SortedMap 。 map 实现类是TreeMap . 如果在模板中,我像这样渲染 map 的值(使用其 t
我正在使用 Thinktecture Identity 服务器并在 azure 中运行它。我遇到的问题是,有时当我读取联合元数据 XML 文件或登录时,我会收到此 Keyset does not ex
我正在使用 Thinktecture Identity 服务器并在 azure 中运行它。我遇到的问题是,有时当我读取联合元数据 XML 文件或登录时,我会收到此 Keyset does not ex
我在 Linux 机器上观察到奇怪的行为。该代码在 Win 7 上运行良好。有问题的代码卡在 xmlToJavaMap.keySet() 上。两条日志语句都没有记录!堆转储中未发现死锁。 Co
这个问题已经有答案了: What is a raw type and why shouldn't we use it? (16 个回答) 已关闭 7 年前。 我试图理解以下代码,其中 acct1和ac
我是 Hazelcast 的新手,我正在尝试使用它来将数据存储在太大而无法容纳在单台计算机上的 map 中。 我需要实现的过程之一是检查 map 中的每个值并对其执行某些操作 - 不是累积或聚合,而且
我最近更新了两个 Windows 控制台应用程序的证书。过去我们刚刚续订了证书,但这次不可能,因为我们的第 3 方提供商不再允许这样做。这次创建了一个名称略有修改的新证书。两个应用程序使用此证书,一个
很迷茫。新建项目,添加这段代码: import org.json.JSONObject; JSONObject jsonObject = new JSONObject(); jsonObject.ke
这个问题在这里已经有了答案: assign instance field to local variable (2 个答案) 关闭 5 年前。 查看源码java.util.HashMap,看到如下代
哪个对性能更好Java 集合中的 entryset() 或 keyset()? (I) 在每个循环中使用 enrtySet() for (Map.Entry entry : testMap.entry
这一定是相当常见的情况,我有一个 map 并希望以线程安全的方式公开其键集: public MyClass { Map map = // ... public final Set keys()
我有以下 Java 代码: public void myMethod (final Map pFeatureGroupsFromPackage) { final Set keys = pFeat
我有 Map> map现在,将两个值放在 Map 中,现在当从 map 读取键集时,在不同的设备中以不同的顺序给出。 private Map> map = new HashMap<>(); map.
这个问题在这里已经有了答案: Why is the local variable ks declared in the HashMap.keySet()? [duplicate] (2 个回答) 10
我想打印我的 HashMap“allDishes”的键。该 HashMap 包含一个 Dish 作为值。Dish 类有一个名为“Ingredients”的 HashMap 字段。 我想打印“allDi
我目前正在尝试访问第三方提供的服务。他们向我们颁发了 PKCS 格式的证书。证书安装在本地计算机 - 受信任的根目录中。 我们的应用程序在运行时找到此证书并将其发送到由第 3 方托管的身份验证 URL
这个问题已经有答案了: Iterating HashMap on order it has been set (2 个回答) Is the order of values retrieved from
我正在编写一个从 LinkedHashMap 列表 中提取键的程序。然后提取的键将用逗号连接起来,并打印为 CSV 文件中的列标题。 flatJson 的示例内容如下所示: { Submitted
我是一名优秀的程序员,十分优秀!