hashtable

Why is value comparison in hashtable returning false even when the values are the same?

自作多情 提交于 2019-12-01 23:21:47
问题 In the following code, I am trying to check if two strings are anagrams. To that, I am counting the characters in the two strings in a hash table by storing the unique characters as key and its count in the string as values. At the end, when I go to check if each of the character have the same count, I get a false output, see the line marked as "PROBLEM" in the code. But when I convert the values in that line to string, the code works fine. What am I missing? static bool AreAnagrams(string

Towards understanding dictionaries

大兔子大兔子 提交于 2019-12-01 23:18:53
问题 I am required to use multiple hashtables, so in c++, I would normally use an std::unordered_map. So far I can understand that I can use a dictionary in Python, so let's assume the following code: my_dict_1 = {} my_dict_1['foo'] = 1 my_dict_2 = {} my_dict_2['foo'] = 2 Will the two dictionaries be using different hash functions (notice that the key is the same ), thus they can be considered two different hash tables (I mean that they will actually store the data differently)? EDIT: Yes the

Towards understanding dictionaries

◇◆丶佛笑我妖孽 提交于 2019-12-01 22:49:46
I am required to use multiple hashtables, so in c++ , I would normally use an std::unordered_map . So far I can understand that I can use a dictionary in Python, so let's assume the following code: my_dict_1 = {} my_dict_1['foo'] = 1 my_dict_2 = {} my_dict_2['foo'] = 2 Will the two dictionaries be using different hash functions (notice that the key is the same ), thus they can be considered two different hash tables (I mean that they will actually store the data differently)? EDIT: Yes the dictionaries are two different objects of course, but the question is about the technique that they will

Why is value comparison in hashtable returning false even when the values are the same?

落爺英雄遲暮 提交于 2019-12-01 22:22:35
In the following code, I am trying to check if two strings are anagrams. To that, I am counting the characters in the two strings in a hash table by storing the unique characters as key and its count in the string as values. At the end, when I go to check if each of the character have the same count, I get a false output, see the line marked as "PROBLEM" in the code. But when I convert the values in that line to string, the code works fine. What am I missing? static bool AreAnagrams(string input1, string input2) { Hashtable uniqueChars1 = new Hashtable(); Hashtable uniqueChars2 = new Hashtable

HashMap vs. TreeMap vs. Hashtable vs.LinkedHashMap

纵然是瞬间 提交于 2019-12-01 20:58:31
1. Map概览 Java SE中有四种常见的Map实现——HashMap, TreeMap, Hashtable和LinkedHashMap。如果我们使用一句话来分别概括它们的特点,就是: HashMap就是一张hash表,键和值都没有排序。 TreeMap以红-黑树结构为基础,键值按顺序排列。 LinkedHashMap保存了插入时的顺序。 Hashtable是同步的(而HashMap是不同步的)。所以如果在线程安全的环境下应该多使用HashMap,而不是Hashtable,因为Hashtable对同步有额外的开销。 HashMap 如果HashMap的键(key)是自定义的对象,那么需要 按规则定义它的equals()和hashCode()方法 。 class Dog { String color; Dog(String c) { color = c; } public String toString(){ return color + " dog"; } } public class TestHashMap { public static void main(String[] args) { HashMap hashMap = new HashMap(); Dog d1 = new Dog("red"); Dog d2 = new Dog("black"); Dog d3

Key for maximum value in Hashtable

此生再无相见时 提交于 2019-12-01 20:55:34
问题 Hi I have the following object: Hashtable<Object, Double> and I want to find the key of the maximum Double value in the table. Easiest way to do that? Thanks 回答1: There is no built in function to get the maximum value out of a Hashtable you are going to have to loop over all the keys and manually determine the max. Object maxKey=null; Double maxValue = Double.MIN_VALUE; for(Map.Entry<Object,Double> entry : table.entrySet()) { if(entry.getValue() > maxValue) { maxValue = entry.getValue();

Python hash table for fuzzy matching

冷暖自知 提交于 2019-12-01 20:52:42
I am trying to implement a data structure which allows rapid look-ups based on keys. The python dict is great when my look-ups involve an equality (e.g. key == somevalue translates to datadict[somevalue] . The problem is that I also need to be able to efficiently look up keys based on a more complex comparison, e.g. key > 50 , or key.startswith('abc') . Obviously I can't use the same solution in both cases, but at the moment I can't figure out how to solve either case. Can anyone suggest a way of doing this? Jon Clements It doesn't sound like you want a hash algorithm - instead some form of

hash tables in powershell

喜夏-厌秋 提交于 2019-12-01 19:20:22
I'm developing an application in PowerShell. I am storing variables in a hashtable. How can I keep the order in the hashtable? I want the order to be the same as I when I filled the hashtable. ravikanth Hash tables by nature don't maintain the order of values. There are a few workarounds already out on the net. Check these http://www.tellingmachine.com/post/2009/01/When-PowerShell-hash-table-magic-backfires.aspx http://huddledmasses.org/powershell-and-hashtable-oddities/ Or Try PS C:\WINDOWS\system32> $OrderedList = New-Object System.Collections.Specialized.OrderedDictionary PS C:\WINDOWS

How many hash buckets does a .net Dictionary use?

♀尐吖头ヾ 提交于 2019-12-01 18:28:48
问题 I know that this is an implementation detail, but I'm curious: Is there a bound on the number of hash buckets used in a .NET Dictionary? I assume that it will be somewhere around 2 * numberOfElements, but does anyone know for sure (or is it documented anywhere)? 回答1: In short: it uses a size equal to the first prime number greater than [numberOfElements]. However it does not consider every prime number: it uses a table for sizes upto a limit, and for bigger sizes it computes a prime number

How to merge two arrays into a map using Java streams?

牧云@^-^@ 提交于 2019-12-01 18:00:43
问题 Lets suppose we were given the following two arrays String[] keys = new String[] {"a", "b", "c", "aa", "d", "b"} int[] values = new int[] { 1 , 2 , 3 , 4 , 5 , 6 } And by merging these 2 arrays into HashTable we get the following // pseudo-code Map<String, Integer> dictionary = new HashTable<>( ("a" => 1) ("b" => 8) // because "b" appeared in index 1 and 5 ("c" => 3) ("aa" => 4) ("d" => 5) ); How can we do this using java Lambda style? So far I have the following: // this loops through the