hashtable

Hash table vs Hash list vs Hash tree?

久未见 提交于 2019-12-03 03:47:36
问题 What property makes Hash table, Hash list and Hash tree different from each other? Which one is used when? When is table superior than tree. 回答1: Hashtable : it's a data structure in which you can insert pairs of (key, value) in which the key is used to compute an hashcode that is needed to decide where to store the value associated with its key. This kind of structure is useful because calculating a hashcode is O(1), so you can find or place an item in constant time. (Mind that there are

HashTable with expirable items

a 夏天 提交于 2019-12-03 03:47:15
I want to implement a HashTable (or mabybe a HashSet or Dictionary ) which has unique members which expire after a while. For example: // Items expire automatically after 10 seconds (Expiration period = 10 sec) bool result = false; // Starting from second 0 result = MyHashSet.Add("Bob"); // second 0 => true result = MyHashSet.Add("Alice"); // second 5 => true result = MyHashSet.Add("Bob"); // second 8 => false (item already exist) result = MyHashSet.Add("Bob"); // second 12 => true (Bob has expired) How to do that in a thread-safe manner with lowest costs? You could create you own Hash Table

What does it mean for a hash function to be incremental?

你。 提交于 2019-12-03 03:36:31
I have heard that, for example, MurmurHash2 is not "incremental" but MurmurHash3 is incremental. What does this mean? And why is it useful? Incremental hash functions suited for situations where if a previously hashed message, M is slightly updated into a new message, M*, then it should be fairly quick to compute the hash value of the updated message, M*. This is done by computing the new hash, m*, from the old hash value, m, in contrast to conventional hash functions that have to recompute the new hash, m* from scratch, which takes a longer time. http://www.cs.berkeley.edu/~daw/papers/inchash

What is the space complexity of a hash table?

梦想与她 提交于 2019-12-03 03:27:29
What is size of a hash table with 32 bit key and 32 bit pointers to values stored separately? Is it going to be 2^32 slots * (4 Bytes (key) + 4 Bytes (pointers to values)) = 4 * 10^9 * (4 + 4) = 32GB ? I am trying to understand space complexity of hash tables. DigitalRoss Hash tables don't match hash function values and slots. The hash function is computed modulo the size of a reference vector that is much smaller than the hash function range. Because this value is fixed, it is not considered in the space complexity computation. Consequently, the space complexity of every reasonable hash table

how to find and return objects in java hashset

一曲冷凌霜 提交于 2019-12-03 03:24:23
According to the HashSet javadoc, HashSet.contains only returns a boolean. How can I "find" an object in a hashSet and modify it (it's not a primitive data type)? I see that HashTable has a get() method, but I would prefer to use the set. You can remove an element and add a different one. Modifying an object while it is in a hash set is a recipe for disaster (if the modification changes the hash value or equality behavior). To quote the source of the stock Sun java.util.HashSet: public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable { static final

Best structure for list of key-value (integer, string) to be shuffled

大城市里の小女人 提交于 2019-12-03 03:10:53
I need to implement a structure in Java that is a key-value list (of types Integer-String) and I want to shuffle it. Basically, I would like to do something like that. public LinkedHashMap<Integer, String> getQuestionOptionsMap(){ LinkedHashMap<Integer, String> shuffle = new LinkedHashMap<Integer, String> (); if (answer1 != null) shuffle.put(new Integer(1), answer1); if (answer2 != null) shuffle.put(new Integer(2), answer2); if (answer3 != null) shuffle.put(new Integer(3), answer3); if (answer4 != null) shuffle.put(new Integer(4), answer4); Collections.shuffle(shuffle); return shuffle; }

What do you exactly mean by HashMap's iterator is fail-fast and HashTable's enumerator isn't?

偶尔善良 提交于 2019-12-03 03:10:19
I was looking up the difference between the two classes and this point came up in a lot of the answers with this blog being the source: http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html However I don't completely get it. Can someone elaborate this? Perhaps with an example? Thanks for looking in! evanwong Fail-fast means when you try to modify the content when you are iterating thru it, it will fail and throw ConcurrentModificationException. Set keys = hashMap.keySet(); for (Object key : keys) { hashMap.put(someObject, someValue); //it will throw the

What are hashtables and hashmaps and their typical use cases?

岁酱吖の 提交于 2019-12-03 02:16:32
问题 I have recently run across these terms few times but I am quite confused how they work and when they are usualy implemented? 回答1: Well, think of it this way. If you use an array, a simple index-based data structure, and fill it up with random stuff, finding a particular entry gets to be a more and more expensive operation as you fill it with data, since you basically have to start searching from one end toward the other, until you find the one you want. If you want to get faster access to

Why is the initialCapacity of Hashtable 11 while the DEFAULT_INITIAL_CAPACITY in HashMap is 16 and requires a power of 2?

醉酒当歌 提交于 2019-12-03 01:58:32
问题 Comparing the HashMap and Hashtable source code in JDK 1.6, I saw the below code inside HashMap: /** * The default initial capacity - MUST be a power of two. */ static final int DEFAULT_INITIAL_CAPACITY = 16; int capacity = 1; while (capacity < initialCapacity) capacity <<= 1; However, in Hashtable, I saw this: table = new Entry[initialCapacity]; public Hashtable() { this(11, 0.75f); } So my question is: Why does HashMap require a power of 2 as the initial capacity, while Hashtable chooses 11

Map list onto dictionary

随声附和 提交于 2019-12-03 01:16:16
Is there a way to map a list onto a dictionary? What I want to do is give it a function that will return the name of a key, and the value will be the original value. For example; somefunction(lambda a: a[0], ["hello", "world"]) => {"h":"hello", "w":"world"} (This isn't a specific example that I want to do, I want a generic function like map() that can do this) I don't think a standard function exists that does exactly that, but it's very easy to construct one using the dict builtin and a comprehension: def somefunction(keyFunction, values): return dict((keyFunction(v), v) for v in values)