简体   繁体   English

没有值的Java哈希映射?

[英]Java hashmaps without the value?

Let's say I want to put words in a data structure and I want to have constant time lookups to see if the word is in this data structure. 假设我想在数据结构中添加单词,并且我希望有恒定时间查找以查看该单词是否在此数据结构中。 All I want to do is to see if the word exists. 我想做的就是看看这个词是否存在。 Would I use a HashMap (containsKey()) for this? 我会为此使用HashMap (containsKey())吗? HashMap s use key->value pairings, but in my case I don't have a value. HashMap使用key-> value配对,但在我的情况下,我没有值。 Of course I could use null for the value, but even null takes space. 当然我可以使用null作为值,但即使null也需要空格。 It seems like there ought to be a better data structure for this application. 看起来这个应用程序应该有更好的数据结构。

The collection could potentially be used by multiple threads, but since the objects contained by the collection would not change, I do not think I have a synchronization/concurrency requirement. 该集合可能会被多个线程使用,但由于集合中包含的对象不会更改,因此我认为我没有同步/并发要求。

Can anyone help me out? 谁能帮我吗?

Use HashSet instead. 请改用HashSet It's a hash implementation of Set , which is used primarily for exactly what you describe (an unordered set of items). 它是Set的哈希实现,主要用于您描述的内容(无序的项集)。

You probably want to use a java.util.Set . 您可能想要使用java.util.Set Implementations include java.util.HashSet , which is the Set equivalent of HashMap. 实现包括java.util.HashSet ,它是HashMap的Set等价物。

Even if the objects contained in the collection do not change, you may need to do synchronization. 即使集合中包含的对象没有更改,您也可能需要进行同步。 Do new objects need to be added to the Set after the Set is passed to a different thread? 将Set传递给另一个线程后,是否需要将新对象添加到Set中? If so, you can use Collections.synchronizedSet() to make the Set thread-safe. 如果是这样,您可以使用Collections.synchronizedSet()使Set线程安全。

If you have a Map with values, and you have some code that just wants to treat the Map as a Set, you can use Map.entrySet() (though keep in mind that entrySet returns a Set view of the keys in the Map; if the Map is mutable, the Map can be changed through the set returned by entrySet). 如果你有一个带有值的Map,并且你有一些代码只想将Map视为一个Set,你可以使用Map.entrySet()(但请记住,entrySet返回Map中键的Set视图;如果Map是可变的,则可以通过entrySet返回的集合来更改Map。

You'd generally use an implementation of Set , and most usually HashSet. 您通常使用Set的实现,通常使用HashSet。 If you did need concurrent access, then ConcurrentHashSet provides a drop-in replacement that provides safe, concurrent access, including safe iteration over the set. 如果确实需要并发访问,那么ConcurrentHashSet提供了一个直接替换,它提供安全的并发访问,包括对集合的安全迭代。

I'd recommend in any case referring to it as simply a Set throughout your code, except in the one place where you construct it; 在任何情况下,我都建议在整个代码中将其称为Set,除非在构建它的地方; that way, it's easier to drop in one implementation for the other if you later require it. 这样,如果您以后需要,可以更容易地为另一个实现一个实现。

Even if the set is read-only , if it's used by a thread other than the one that creates it, you do need to think about safe publication (that is, making sure that any other thread sees the set in a consistent state: remember any memory writes, even in constructors, aren't guaranteed to be made available to other threads when or in the otder you expect, unless you take steps to ensure this). 即使该集合是只读的 ,如果它由创建它的线程以外的线程使用,您也需要考虑安全发布 (即,确保任何其他线程看到该集合处于一致状态:记住任何内存写入,即使在构造函数中,也不保证在您期望的时候或者在其他线程中可用,除非您采取措施来确保这一点)。 This can be done by both of the following: 这可以通过以下两种方式完成:

  • making sure the only reference(s) to the set are in final fields ; 确保集合中的唯一引用位于最终字段中 ;
  • making sure that it really is true that no thread modifies the set. 确保没有线程修改集合确实是真的。

You can help to ensure the latter by using the Collections.unmodifiableSet() wrapper. 您可以使用Collections.unmodifiableSet()包装器来帮助确保后者。 This gives you an unmodifiable view of the given set-- so provided no other "normal" reference to the set escapes, you're safe. 这给你一个给定集合的不可修改的视图 - 因此没有提供对集合转义的其他“正常”引用,你是安全的。

You want to use a Collection implementing the Set interface, probably HashSet to get the performance you stated. 您希望使用实现Set接口的Collection,可能使用HashSet来获得您声明的性能。 See http://java.sun.com/javase/6/docs/api/java/util/Set.html 请参阅http://java.sun.com/javase/6/docs/api/java/util/Set.html

除了Set S,在某些情况下,你可能要一个转换MapSetCollections.newSetFromMap(Map<E,Boolean>)一些Map小号不允许null值,因此Boolean )。

as everyone said HashSet is probably the simplest solution but you won't have constant time lookup in a HashSet (because entries may be chained) and you will store a dummy object (always the same) for every entry... 正如大家所说HashSet可能是最简单的解决方案,但你不会在HashSet中进行恒定的时间查找(因为条目可能被链接)并且你将为每个条目存储一个虚拟对象(总是相同的)...

For information here a list of data structures maybe you'll find one that better fits your needs. 有关此处的信息您可以找到更符合您需求的数据结构列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中的double数组中查找最频繁的值(无哈希图或排序) - Find the most frequent value in an array of double in Java (without hashmaps or sorting) 从Java中的hashMap列表的值获取键 - Get the key from the value of a list of hashMaps in java 遍历hashmaps的hashmap以在Java中检索字符串值 - Walking a hashmap of hashmaps to retrieve a string value in Java Java哈希图,其中一个键为另一个的值 - Java hashmaps with Key of one as value of other 双精度数组-最频繁的取值方法? (没有哈希图或排序) - Array of doubles — most frequent value method? (without hashmaps or sorting) 在 Java 中嵌套 collections(包含字符串键的哈希图列表,arrayList 值) - Nesting collections in Java (list of hashmaps containing String key, arrayList value) HashMap 键和值等于使用 Java 的其他两个哈希图的值 - HashMap with key and value equal the values of two other hashmaps using Java Java-在通过HashMap进行迭代时保留值的数据类型 - Java - Retain data type of value while iterating through HashMaps 如何对Java中不同键的哈希映射列表中的列值求和 - How to sum a column value in a list of hashmaps against different Keys in java Java Hibernate-是否在Hashmap中保留Hashmap? - Java Hibernate - Persisting Hashmaps within Hashmaps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM