hashtable

Sort Hashtable by (possibly non-unique) values

ⅰ亾dé卋堺 提交于 2019-12-04 13:08:44
I have a Hashtable that maps strings to ints. Strings are unique, but several may be mapped to the same integer. My naive approach was to simply invert the Hashtable to a SortedList that is indexed by the Hashtable's values, but the problem is that you get a clash as soon as two of the Hashtable's strings map to the same value. What is the most efficient way to list my entire Hashtable (keys and values) ordered by the values? (Where two values are the same, I don't care about their ordering.) Using Linq: hashtable.Cast<DictionaryEntry>().OrderBy(entry => entry.Value).ToList() You said you

Retrieving hashmap values in XSLT

感情迁移 提交于 2019-12-04 12:39:16
I am executing an XSLT transformation using java program. Given below is the block of code that is used for transformation. Here I am creating a hashmap and setting a value which needs to be accessed in the XSLT. TransformerFactory factory = TransformerFactory.newInstance(); StreamSource xslStream = new StreamSource(inputXSL); Transformer transformer = factory.newTransformer(xslStream); Map<String,String> mapData = new HashMap<String,String>(); mapData.put("103", "188 E 6th Street"); transformer.setParameter("mapData", mapData); The xslt code(inputXSL) that does the transformation is shown

Best way to initialize a HashMap

让人想犯罪 __ 提交于 2019-12-04 12:34:02
I usually do e.g. HashMap<String,String> dictionary = new HashMap<String,String>(); I started to think about it, and as far as I know a HashMap is implemented under the hood via a hash table. The objects are stored in the table using a hash to find where they should be stored in the table. Does the fact that I do not set a size on the construction of the dictionary makes the performace decrease? I.e. what would be the size of the hash table during construction? Would it need to allocate new memory for the table as elements increase? Or I am confused on the concept here? Are the default

how to easily sum two hashMap<String,Integer>?

断了今生、忘了曾经 提交于 2019-12-04 10:38:12
I have two HashMap<String,Integer> How can I sum them easily? Meaning that for String "a" the key will be sum of (value from Map1 + value from Map2)? I can iterate every item of Map2 and add manually to Map1. But thought there might be an easier way? I prefer summing the Integers into one of the maps. Not creating a new one Since Java 8 Map contains merge method which requires key, new value, and function which will be used to decide what value to put in map if it already contains our key (decision will be made based on old and new value). So you could simply use: map2.forEach((k, v) -> map1

Difference between HashMap and HashTable purely in Data Structures

自闭症网瘾萝莉.ら 提交于 2019-12-04 10:29:47
问题 What is the difference between HashTable and HashMap purely in context of data structures (and not in Java or any other language) ? I have seen people using these terms interchangeably for the same concept. Does it have no difference at all purely in context of data structures ? 回答1: In Computing Science terminology, a map is an associative container mapping from a key to a value. In other words, you can do operations like "for key K remember value V" and later "for key K get the value". A

Why use an array to implement a “list” instead of a hash table?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 10:15:18
Consider an array versus a hashtable where the keys are just the integral indexes of a list. Their average-case insertion, lookup, and removal big-O bounds are all O(1) constant time. I understand that you may get some low-level wins in cache locality with an array, and there is a marginal (mostly-constant) overhead to the hashtable operations, but hashtables get you sparseness for free, which in some applications is a big win. What other significant (or small) contrasts am I missing? Context: I get into a discussion about this sometimes when interviewing programming candidates. Usually the

Hash tables v self-balancing search trees

牧云@^-^@ 提交于 2019-12-04 10:15:13
问题 I am curious to know what is the reasoning that could overweighs towards using a self-balancing tree technique to store items than using a hash table. I see that hash tables cannot maintain the insertion-order, but I could always use a linked list on top to store the insertion-order sequence. I see that for small number of values, there is an added cost of of the hash-function, but I could always save the hash-function together with the key for faster lookups. I understand that hash tables

In-place dictionary inversion in Python

六眼飞鱼酱① 提交于 2019-12-04 10:10:27
I need to invert a dictionary of lists, I don't know how to explain it in English exactly, so here is some code that does what I want. It just takes too much memory. def invert(oldDict): invertedDict = {} for key,valuelist in oldDict.iteritems(): for value in valuelist: try: entry = invertedDict[value] if key not in entry: entry.append(key) except KeyError: invertedDict[value] = [key] return invertedDict The original is a dict of lists, and the result is a dict of lists. This "inverts" it. test = {} test[1] = [1999,2000,2001] test[2] = [440,441] test[3] = [440,2000] print invert(test) This

What does “bucket entries” mean in the context of a hashtable?

China☆狼群 提交于 2019-12-04 08:04:58
问题 What does "bucket entries" mean in the context of a hashtable? 回答1: A bucket is simply a fast-access location (like an array index) that is the the result of the hash function. The idea with hashing is to turn a complex input value into a different value which can be used to rapidly extract or store data. Consider the following hash function for mapping people's names into street addresses. First take the initials from the first and last name and turn them both into numeric values (0 through

Hashing Keys in Java

落爺英雄遲暮 提交于 2019-12-04 08:03:29
In java, when I use a String as a key for Hashmap I get a little different result than when I use the string hashcode as a key in the HashMap. Any insight? when I use the string hashcode as a key in the HashMap. You mustn't use the hash code itself as the key. Hash codes aren't intended to be unique - it's entirely permitted for two non-equal values to have the same hash code. You should use the string itself as a key. The map will then compare hash codes first (to narrow down the candidate matches quickly) and then compare with equals for genuine string equality. Of course, that's assuming