hashtable

HashTable with expirable items

青春壹個敷衍的年華 提交于 2019-12-03 13:52:50
问题 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

Berkeleydb - B-Tree versus Hash Table

你说的曾经没有我的故事 提交于 2019-12-03 13:45:18
I am trying to understand what should drive the choice of the access method while using a BerkeleyDB : B-Tree versus HashTable. A Hashtable provides O(1) lookup but inserts are expensive (using Linear/Extensible hashing we get amortized O(1) for insert). But B-Trees provide log N (base B) lookup and insert times. A B-Tree can also support range queries and allow access in sorted order. Apart from these considerations what else should be factored in? If I don't need to support range queries can I just use a Hashtable access method? When your data sets get very large, B-trees are still better

What is the space complexity of a hash table?

只谈情不闲聊 提交于 2019-12-03 12:11:03
问题 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. 回答1: 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

What is the main implementation idea behind sparse hash table?

我们两清 提交于 2019-12-03 10:55:41
Why does Google sparsehash open-source library has two implementations: a dense hashtable and a sparse one? The dense hashtable is your ordinary textbook hashtable implementation. The sparse hashtable stores only the elements that have actually been set, divided over a number of arrays. To quote from the comments in the implementation of sparse tables: // The idea is that a table with (logically) t buckets is divided // into t/M *groups* of M buckets each. (M is a constant set in // GROUP_SIZE for efficiency.) Each group is stored sparsely. // Thus, inserting into the table causes some array

Why are tuples constructed from differently initialized sets equal?

拥有回忆 提交于 2019-12-03 10:29:52
问题 I expected the following two tuples >>> x = tuple(set([1, "a", "b", "c", "z", "f"])) >>> y = tuple(set(["a", "b", "c", "z", "f", 1])) to compare unequal, but they don't: >>> x == y >>> True Why is that? 回答1: At first glance, it appears that x should always equal y , because two sets constructed from the same elements are always equal: >>> x = set([1, "a", "b", "c", "z", "f"]) >>> y = set(["a", "b", "c", "z", "f", 1]) >>> x {1, 'z', 'a', 'b', 'c', 'f'} >>> y {1, 'z', 'a', 'b', 'c', 'f'} >>> x

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

自古美人都是妖i 提交于 2019-12-03 09:48:10
问题 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! 回答1: 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();

iterating through Enumeration of hastable keys throws NoSuchElementException error

天大地大妈咪最大 提交于 2019-12-03 09:17:35
I am trying to iterate through a list of keys from a hash table using enumeration however I keep getting a NoSuchElementException at the last key in list? Hashtable<String, String> vars = new Hashtable<String, String>(); vars.put("POSTCODE","TU1 3ZU"); vars.put("EMAIL","job.blogs@lumesse.com"); vars.put("DOB","02 Mar 1983"); Enumeration<String> e = vars.keys(); while(e.hasMoreElements()){ System.out.println(e.nextElement()); String param = (String) e.nextElement(); } Console output: EMAIL POSTCODE Exception in thread "main" java.util.NoSuchElementException: Hashtable Enumerator at java.util

A faster hash function

只谈情不闲聊 提交于 2019-12-03 09:14:41
I'm trying to implement my own hash function, i add up the ASCII numbers of each string, using java. I find the hash code by finding the mod of the size of the hash table and the sum. size%sum. I was wondering if there was a way to use the same process but reduce collisions, when searching for the string? Thanks in advance. I would look at the code for String and HashMap as these have a low collision rate and don't use % and handle negative numbers. From the source for String public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value

What is the easiest way to sort maps according to values in Java?

巧了我就是萌 提交于 2019-12-03 07:36:34
I want my hash to sort in descending order according to the values. How do I do that in Java? A HashMap (and its legacy predecesor Hashtable ) is by nature unordered. Even if you sort it, it will remain unordered. If you want to maintain insertion order, then use LinkedHashMap instead. If you want an automatic sort on keys , regardless of insertion order, then use SortedMap instead. If you want to sort a Map on values , then you basically need to put the key/value pairs in another kind of a sortable data structure, e.g. List<Entry<K, V>> , then sort it using Collections#sort() with help of a

how does the hashCode() method of java works?

我只是一个虾纸丫 提交于 2019-12-03 07:06:24
问题 I am curious how java generates hash values by using hashCode() method of the Object API ? 回答1: Java doesn't generate hashCode(), i.e. nothing automatic is happening here. However, Object generates a HashCode based on the memory address of the instance of the object. Most classes (especially if you are going to use it in any of the Collection API) should implement their own HashCode (and by contract their own equals method). 回答2: The hashCode() of Object is actually a native method and the