是否有一个只有键没有值的 java 哈希结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/460088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a java hash structure with keys only and no values?
提问by
I'm looking for a structure which hashes keys without requiring a value. When queried, it should return true if the key is found and false otherwise. I'm looking for something similar to
Hashtable<MyClass, Boolean>
except insertion requires only a key and queries only ever return true or false, never null.
我正在寻找一种不需要值就可以对键进行散列的结构。查询时,如果找到键,则返回 true,否则返回 false。我正在寻找类似于
Hashtable<MyClass, Boolean>
除了插入只需要一个键和查询只返回真或假,从不为空的东西。
回答by Gant
You need Java's HashSet(Java 8).
The description from the official documentationis:
官方文档中的描述是:
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
Note that this implementation is not synchronized. If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:
Set s = Collections.synchronizedSet(new HashSet(...));
The iterators returned by this class's iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the Iterator throws a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
This class is a member of the Java Collections Framework.
这个类实现了 Set 接口,由一个哈希表(实际上是一个 HashMap 实例)支持。它不保证集合的迭代顺序;特别是,它不保证订单会随着时间的推移保持不变。此类允许空元素。
这个类为基本操作(添加、删除、包含和大小)提供恒定的时间性能,假设散列函数在桶中正确地分散元素。迭代这个集合需要的时间与 HashSet 实例的大小(元素数)加上支持 HashMap 实例的“容量”(桶数)的总和成正比。因此,如果迭代性能很重要,则不要将初始容量设置得太高(或负载因子太低),这一点非常重要。
请注意,此实现不是同步的。如果多个线程同时访问一个散列集,并且至少有一个线程修改了该集,则必须在外部进行同步。这通常是通过同步一些自然封装集合的对象来实现的。如果不存在这样的对象,则应使用 Collections.synchronizedSet 方法“包装”该集合。这最好在创建时完成,以防止对集合的意外不同步访问:
Set s = Collections.synchronizedSet(new HashSet(...));
此类的迭代器方法返回的迭代器是快速失败的:如果在迭代器创建后的任何时间修改了集合,除了通过迭代器自己的 remove 方法之外的任何方式,迭代器都会抛出 ConcurrentModificationException。因此,面对并发修改,迭代器快速而干净地失败,而不是冒着在未来不确定的时间出现任意、非确定性行为的风险。
请注意,无法保证迭代器的快速失败行为,因为一般而言,在存在非同步并发修改的情况下不可能做出任何硬保证。快速失败的迭代器会尽最大努力抛出 ConcurrentModificationException。因此,编写一个依赖此异常来确保其正确性的程序是错误的:迭代器的快速失败行为应该仅用于检测错误。
此类是 Java 集合框架的成员。
回答by Lawrence Dol
java.util.HashSet? Using contains() for your lookup.
java.util.HashSet?使用 contains() 进行查找。
回答by akuhn
See also the static methods Collections#newSetFromMap
that creates a set based on the given map implementation. This is eg handy to create a weak hash set.
另请参阅Collections#newSetFromMap
基于给定地图实现创建集合的静态方法。例如,这对于创建弱散列集很方便。
回答by Ashok Koyi
Java Set is designed to remove duplicates and hopefully the HashMap must be using Java Setinternally for managing key as keys can never have duplicates, So you should be considering set for your requirement.
Java Set 旨在删除重复项,希望 HashMap 必须在内部使用 Java Set来管理密钥,因为密钥永远不会有重复项,因此您应该考虑根据您的要求设置。