有没有java哈希结构只有键,没有值? [英] Is there a java hash structure with keys only and no values?

查看:125
本文介绍了有没有java哈希结构只有键,没有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个不需要值的哈希键的结构。查询时,如果找到键,则返回true,否则返回false。我正在寻找类似的东西
Hashtable< MyClass,Boolean>
除了插入只需要一个键和查询只返回true或false,从不为空。

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.

推荐答案

您需要Java的 HashSet

网站的描述是:


此类实现了Set接口,由散列表
(实际上是一个HashMap实例)支持。它不保证集合的
迭代顺序;特别是,它不保证
的顺序将随时间保持不变。

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.

此类提供基本操作的常量时间性能
(add,remove,contains和size)假设散列函数在元组中正确分散元素
。迭代这个集合
需要与HashSet实例的大小
(元素数量)加上支持HashMap
实例(桶的数量)的capacity成正比的时间。因此,如果
迭代性能很重要,不要将
设置为初始容量过高(或负载因子太低)。

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.

请注意,此实现未同步。如果多个线程
并发访问一个哈希集,并且线程
中的至少一个修改了该集合,那么它必须在外部同步。这是
通常通过同步在某些对象上完成,自然
封装集合。如果没有这样的对象存在,集合应该是
wrapped使用Collections.synchronizedSet方法。这是最好的
在创建时完成,以防止意外的不同步访问
集合:

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:

设置s = Collections.synchronizedSet(new HashSet ...));

Set s = Collections.synchronizedSet(new HashSet(...));

这个类的迭代器方法返回的迭代器是快速失败的:
如果在迭代器之后的任何时候修改集合创建,在
任何方式除了通过迭代器自己的remove方法,Iterator
抛出一个ConcurrentModificationException。因此,面对
并发修改,迭代器失败的快速和干净
,而不是冒险在未来的一个
未确定的时间的任意,非确定性行为。

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.

请注意,迭代器的fail-fast行为不能保证
,因为一般来说,在不同步并发的情况下不可能提供任何硬保证
修改。失败快速
迭代器在尽力而为
基础上抛出ConcurrentModificationException。因此,编写一个依赖于
的程序是不正确的:
迭代器的fail-fast行为只能用于检测错误。

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.

此类是Java Collections Framework的成员。

This class is a member of the Java Collections Framework.

这篇关于有没有java哈希结构只有键,没有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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